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

Method to_mongo

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

Source from the content-addressed store, hash-verified

1522 )
1523
1524 def to_mongo(self, document):
1525 if document is None:
1526 return None
1527
1528 if isinstance(document, (dict, SON, ObjectId, DBRef)):
1529 return document
1530
1531 id_field_name = document.__class__._meta["id_field"]
1532 id_field = document.__class__._fields[id_field_name]
1533
1534 if isinstance(document, Document):
1535 # We need the id from the saved object to create the DBRef
1536 id_ = document.id
1537 if id_ is None:
1538 # XXX ValidationError raised outside of the "validate" method.
1539 self.error(
1540 "You can only reference documents once they have"
1541 " been saved to the database"
1542 )
1543 else:
1544 id_ = document
1545
1546 id_ = id_field.to_mongo(id_)
1547 collection = document._get_collection_name()
1548 ref = DBRef(collection, id_)
1549 return SON((("_cls", document._class_name), ("_ref", ref)))
1550
1551 def prepare_query_value(self, op, value):
1552 if value is None:

Callers 1

prepare_query_valueMethod · 0.95

Calls 3

errorMethod · 0.80
to_mongoMethod · 0.45
_get_collection_nameMethod · 0.45

Tested by

no test coverage detected