MCPcopy Index your code
hub / github.com/MongoEngine/mongoengine / __set__

Method __set__

mongoengine/base/fields.py:176–210  ·  view source on GitHub ↗

Descriptor for assigning a value to a field in a document.

(self, instance, value)

Source from the content-addressed store, hash-verified

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."""

Callers 1

__set__Method · 0.45

Calls 2

_import_classFunction · 0.90
_mark_as_changedMethod · 0.45

Tested by

no test coverage detected