Reference field¶
Work with references.
class RDoc(Document):
i = fields.IntegerField()
class Doc(Document):
rdoc = fields.ReferenceField(RDoc)
rdoc = RDoc()
rdoc.i = 13
db.insert(rdoc)
doc = Doc()
doc.rdoc = rdoc
db.insert(doc)
doc = db.get_queryset(Doc).find_one(doc.id) # reload doc
assert doc.rdoc.id == rdoc.id
assert doc.rdoc.i == 13
Or with asyncio:
rdoc = await doc.rdoc
assert rdoc.id == rdoc.id
assert rdoc.i == 13
assert doc.rdoc == rdoc.id
-
exception
yadm.fields.reference.
BrokenReference
¶ Raise if referrenced document is not found.
-
exception
yadm.fields.reference.
NotBindingToDatabase
¶ Raise if set ObjectId insted referenced document to new document, who not binded to database.
-
class
yadm.fields.reference.
Reference
(_id: bson.objectid.ObjectId, parent: yadm.documents.DocumentItemMixin, field: yadm.fields.reference.ReferenceField)¶ Reference object.
This is awaitable:
doc = await doc.reference
-
class
yadm.fields.reference.
ReferenceField
(reference_document_class, **kwargs)¶ Field for work with references.
Parameters: reference_document_class – class for refered documents -
copy
()¶ Return copy of field.
-
from_mongo
(document, value)¶ Convert mongo value to python value.
Parameters: - document (BaseDocument) – document
- value – mongo value
Returns: python value
-
get_default
(document)¶ Return default value.
-
get_fake
(document, faker, depth)¶ Try create referenced document.
-
prepare_value
(document, value)¶ The method is called when value is assigned for the attribute.
Parameters: - document (BaseDocument) – document
- value – raw value
Returns: prepared value
It must be accept value argument and return processed (e.g. casted) analog. Also it is called once for the default value.
-
to_mongo
(document, value)¶ Convert python value to mongo value.
Parameters: - document (BaseDocument) – document
- value – python value
Returns: mongo value
-