Descriptor for assigning a value to a field in a document.
(self, instance, value)
| 174 | return instance._data.get(self.name) |
| 175 | |
| 176 | def __set__(self, instance, value): |
| 177 | """Descriptor for assigning a value to a field in a document.""" |
| 178 | # If setting to None and there is a default value provided for this |
| 179 | # field, then set the value to the default value. |
| 180 | if value is None: |
| 181 | if self.null: |
| 182 | value = None |
| 183 | elif self.default is not None: |
| 184 | value = self.default |
| 185 | if callable(value): |
| 186 | value = value() |
| 187 | |
| 188 | if instance._initialised: |
| 189 | try: |
| 190 | value_has_changed = ( |
| 191 | self.name not in instance._data |
| 192 | or instance._data[self.name] != value |
| 193 | ) |
| 194 | if value_has_changed: |
| 195 | instance._mark_as_changed(self.name) |
| 196 | except Exception: |
| 197 | # Some values can't be compared and throw an error when we |
| 198 | # attempt to do so (e.g. tz-naive and tz-aware datetimes). |
| 199 | # Mark the field as changed in such cases. |
| 200 | instance._mark_as_changed(self.name) |
| 201 | |
| 202 | EmbeddedDocument = _import_class("EmbeddedDocument") |
| 203 | if isinstance(value, EmbeddedDocument): |
| 204 | value._instance = weakref.proxy(instance) |
| 205 | elif isinstance(value, (list, tuple)): |
| 206 | for v in value: |
| 207 | if isinstance(v, EmbeddedDocument): |
| 208 | v._instance = weakref.proxy(instance) |
| 209 | |
| 210 | instance._data[self.name] = value |
| 211 | |
| 212 | def error(self, message="", errors=None, field_name=None): |
| 213 | """Raise a ValidationError.""" |
no test coverage detected