Cleaner code & comments in BaseField.__set__
This commit is contained in:
parent
216217e2c6
commit
a4fe091a51
@ -128,10 +128,9 @@ class BaseField(object):
|
|||||||
return instance._data.get(self.name)
|
return instance._data.get(self.name)
|
||||||
|
|
||||||
def __set__(self, instance, value):
|
def __set__(self, instance, value):
|
||||||
"""Descriptor for assigning a value to a field in a document.
|
"""Descriptor for assigning a value to a field in a document."""
|
||||||
"""
|
# If setting to None and there is a default value provided for this
|
||||||
# If setting to None and there is a default
|
# field, then set the value to the default value.
|
||||||
# Then set the value to the default value
|
|
||||||
if value is None:
|
if value is None:
|
||||||
if self.null:
|
if self.null:
|
||||||
value = None
|
value = None
|
||||||
@ -142,12 +141,16 @@ class BaseField(object):
|
|||||||
|
|
||||||
if instance._initialised:
|
if instance._initialised:
|
||||||
try:
|
try:
|
||||||
if (self.name not in instance._data or
|
value_has_changed = (
|
||||||
instance._data[self.name] != value):
|
self.name not in instance._data or
|
||||||
|
instance._data[self.name] != value
|
||||||
|
)
|
||||||
|
if value_has_changed:
|
||||||
instance._mark_as_changed(self.name)
|
instance._mark_as_changed(self.name)
|
||||||
except Exception:
|
except Exception:
|
||||||
# Values cant be compared eg: naive and tz datetimes
|
# Some values can't be compared and throw an error when we
|
||||||
# So mark it as changed
|
# attempt to do so (e.g. tz-naive and tz-aware datetimes).
|
||||||
|
# Mark the field as changed in such cases.
|
||||||
instance._mark_as_changed(self.name)
|
instance._mark_as_changed(self.name)
|
||||||
|
|
||||||
EmbeddedDocument = _import_class('EmbeddedDocument')
|
EmbeddedDocument = _import_class('EmbeddedDocument')
|
||||||
@ -157,6 +160,7 @@ class BaseField(object):
|
|||||||
for v in value:
|
for v in value:
|
||||||
if isinstance(v, EmbeddedDocument):
|
if isinstance(v, EmbeddedDocument):
|
||||||
v._instance = weakref.proxy(instance)
|
v._instance = weakref.proxy(instance)
|
||||||
|
|
||||||
instance._data[self.name] = value
|
instance._data[self.name] = value
|
||||||
|
|
||||||
def error(self, message='', errors=None, field_name=None):
|
def error(self, message='', errors=None, field_name=None):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user