(self, document)
| 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: |
no test coverage detected