A reference to a document that will be automatically dereferenced on access (lazily). Note this means you will get a database I/O access everytime you access this field. This is necessary because the field returns a :class:`~mongoengine.Document` which precise type can depend of the
| 1098 | |
| 1099 | |
| 1100 | class ReferenceField(BaseField): |
| 1101 | """A reference to a document that will be automatically dereferenced on |
| 1102 | access (lazily). |
| 1103 | |
| 1104 | Note this means you will get a database I/O access everytime you access |
| 1105 | this field. This is necessary because the field returns a :class:`~mongoengine.Document` |
| 1106 | which precise type can depend of the value of the `_cls` field present in the |
| 1107 | document in database. |
| 1108 | In short, using this type of field can lead to poor performances (especially |
| 1109 | if you access this field only to retrieve it `pk` field which is already |
| 1110 | known before dereference). To solve this you should consider using the |
| 1111 | :class:`~mongoengine.fields.LazyReferenceField`. |
| 1112 | |
| 1113 | Use the `reverse_delete_rule` to handle what should happen if the document |
| 1114 | the field is referencing is deleted. EmbeddedDocuments, DictFields and |
| 1115 | MapFields does not support reverse_delete_rule and an `InvalidDocumentError` |
| 1116 | will be raised if trying to set on one of these Document / Field types. |
| 1117 | |
| 1118 | The options are: |
| 1119 | |
| 1120 | * DO_NOTHING (0) - don't do anything (default). |
| 1121 | * NULLIFY (1) - Updates the reference to null. |
| 1122 | * CASCADE (2) - Deletes the documents associated with the reference. |
| 1123 | * DENY (3) - Prevent the deletion of the reference object. |
| 1124 | * PULL (4) - Pull the reference from a :class:`~mongoengine.fields.ListField` of references |
| 1125 | |
| 1126 | Alternative syntax for registering delete rules (useful when implementing |
| 1127 | bi-directional delete rules) |
| 1128 | |
| 1129 | .. code-block:: python |
| 1130 | |
| 1131 | class Org(Document): |
| 1132 | owner = ReferenceField('User') |
| 1133 | |
| 1134 | class User(Document): |
| 1135 | org = ReferenceField('Org', reverse_delete_rule=CASCADE) |
| 1136 | |
| 1137 | User.register_delete_rule(Org, 'owner', DENY) |
| 1138 | """ |
| 1139 | |
| 1140 | def __init__( |
| 1141 | self, document_type, dbref=False, reverse_delete_rule=DO_NOTHING, **kwargs |
| 1142 | ): |
| 1143 | """Initialises the Reference Field. |
| 1144 | |
| 1145 | :param document_type: The type of Document that will be referenced |
| 1146 | :param dbref: Store the reference as :class:`~pymongo.dbref.DBRef` |
| 1147 | or as the :class:`~pymongo.objectid.ObjectId`. |
| 1148 | :param reverse_delete_rule: Determines what to do when the referring |
| 1149 | object is deleted |
| 1150 | :param kwargs: Keyword arguments passed into the parent :class:`~mongoengine.BaseField` |
| 1151 | |
| 1152 | .. note :: |
| 1153 | A reference to an abstract document type is always stored as a |
| 1154 | :class:`~pymongo.dbref.DBRef`, regardless of the value of `dbref`. |
| 1155 | """ |
| 1156 | # XXX ValidationError raised outside of the "validate" method. |
| 1157 | if not ( |