MCPcopy Index your code
hub / github.com/MongoEngine/mongoengine / to_mongo

Method to_mongo

mongoengine/fields.py:1209–1244  ·  view source on GitHub ↗
(self, document)

Source from the content-addressed store, hash-verified

1207 return super().__get__(instance, owner)
1208
1209 def to_mongo(self, document):
1210 if isinstance(document, DBRef):
1211 if not self.dbref:
1212 return document.id
1213 return document
1214
1215 if isinstance(document, Document):
1216 # We need the id from the saved object to create the DBRef
1217 id_ = document.pk
1218
1219 # XXX ValidationError raised outside of the "validate" method.
1220 if id_ is None:
1221 self.error(
1222 "You can only reference documents once they have"
1223 " been saved to the database"
1224 )
1225
1226 # Use the attributes from the document instance, so that they
1227 # override the attributes of this field's document type
1228 cls = document
1229 else:
1230 id_ = document
1231 cls = self.document_type
1232
1233 id_field_name = cls._meta["id_field"]
1234 id_field = cls._fields[id_field_name]
1235
1236 id_ = id_field.to_mongo(id_)
1237 if self.document_type._meta.get("abstract"):
1238 collection = cls._get_collection_name()
1239 return DBRef(collection, id_, cls=cls._class_name)
1240 elif self.dbref:
1241 collection = cls._get_collection_name()
1242 return DBRef(collection, id_)
1243
1244 return id_
1245
1246 def to_python(self, value):
1247 """Convert a MongoDB-compatible type to a Python type."""

Callers 1

prepare_query_valueMethod · 0.95

Calls 4

errorMethod · 0.80
to_mongoMethod · 0.45
getMethod · 0.45
_get_collection_nameMethod · 0.45

Tested by

no test coverage detected