| 1365 | return super().__get__(instance, owner) |
| 1366 | |
| 1367 | def to_mongo(self, document, use_db_field=True, fields=None): |
| 1368 | id_field_name = self.document_type._meta["id_field"] |
| 1369 | id_field = self.document_type._fields[id_field_name] |
| 1370 | |
| 1371 | # XXX ValidationError raised outside of the "validate" method. |
| 1372 | if isinstance(document, Document): |
| 1373 | # We need the id from the saved object to create the DBRef |
| 1374 | id_ = document.pk |
| 1375 | if id_ is None: |
| 1376 | self.error( |
| 1377 | "You can only reference documents once they have" |
| 1378 | " been saved to the database" |
| 1379 | ) |
| 1380 | else: |
| 1381 | self.error("Only accept a document object") |
| 1382 | |
| 1383 | value = SON((("_id", id_field.to_mongo(id_)),)) |
| 1384 | |
| 1385 | if fields: |
| 1386 | new_fields = [f for f in self.fields if f in fields] |
| 1387 | else: |
| 1388 | new_fields = self.fields |
| 1389 | |
| 1390 | value.update(dict(document.to_mongo(use_db_field, fields=new_fields))) |
| 1391 | return value |
| 1392 | |
| 1393 | def prepare_query_value(self, op, value): |
| 1394 | if value is None: |