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

def __set__(self, instance, value):

    if (value is None):
        if self.null:
            value = None
        elif (self.default is not None):
            value = self.default
            if callable(value):
                value = value()
    if instance._initialised:
        try:
            if ((self.name not in instance._data) or (instance._data[self.name] != value)):
                instance._mark_as_changed(self.name)
        except Exception:
            instance._mark_as_changed(self.name)
    EmbeddedDocument = _import_class('EmbeddedDocument')
    if isinstance(value, EmbeddedDocument):
        value._instance = weakref.proxy(instance)
    elif isinstance(value, (list, tuple)):
        for v in value:
            if isinstance(v, EmbeddedDocument):
                v._instance = weakref.proxy(instance)
    instance._data[self.name] = value
