def get_fields(cls, name, fields=None): from motorengine import EmbeddedDocumentField, ListField from motorengine.fields.dynamic_field import DynamicField if fields is None: fields = [] if '.' not in name: dyn_field = DynamicField(db_field="_%s" % name) fields.append(cls._fields.get(name, dyn_field)) return fields field_values = name.split('.') dyn_field = DynamicField(db_field="_%s" % field_values[0]) obj = cls._fields.get(field_values[0], dyn_field) fields.append(obj) if isinstance(obj, (EmbeddedDocumentField, )): obj.embedded_type.get_fields(".".join(field_values[1:]), fields=fields) if isinstance(obj, (ListField, )): obj.item_type.get_fields(".".join(field_values[1:]), fields=fields) return fields
def __init__(self, _is_partly_loaded=False, _reference_loaded_fields=None, **kw): """ :param _is_partly_loaded: is a flag that indicates if the document was loaded partly (with `only`, `exlude`, `fields`). Default: False. :param _reference_loaded_fields: dict that contains projections for reference fields if any. Default: None. :param kw: pairs of fields of the document and their values """ from motorengine.fields.dynamic_field import DynamicField self._id = kw.pop('_id', None) self._values = {} self.is_partly_loaded = _is_partly_loaded if _reference_loaded_fields: self._reference_loaded_fields = _reference_loaded_fields else: self._reference_loaded_fields = {} for key, field in self._fields.items(): if callable(field.default): self._values[field.name] = field.default() else: self._values[field.name] = field.default for key, value in kw.items(): if key not in self._fields: self._fields[key] = DynamicField(db_field="_%s" % key.lstrip('_')) self._values[key] = value
def __init__(self, _is_partly_loaded=False, _reference_loaded_fields=None, **kw): from motorengine.fields.dynamic_field import DynamicField self._id = kw.pop('_id', None) self._values = {} self.is_partly_loaded = _is_partly_loaded if _reference_loaded_fields: self._reference_loaded_fields = _reference_loaded_fields else: self._reference_loaded_fields = {} for key, field in self._fields.items(): if callable(field.default): self._values[field.name] = field.default() else: self._values[field.name] = field.default for key, value in kw.items(): if key not in self._fields: self._fields[key] = DynamicField(db_field="_%s" % key.lstrip('_')) self._values[key] = value
def __setattr__(self, name, value): from motorengine.fields.dynamic_field import DynamicField if name not in AUTHORIZED_FIELDS and name not in self._fields: self._fields[name] = DynamicField(db_field="_%s" % name) if name in self._fields: self._values[name] = value return object.__setattr__(self, name, value)
def __setattr__(self, name, value): from motorengine.fields.dynamic_field import DynamicField if name not in AUTHORIZED_FIELDS and name not in self._fields and name not in DISABLED_FIELDS and self.__allow_undefined_fields__: self._fields[name] = DynamicField(db_field="_%s" % name) if name in self._fields: self._values[name] = value self._changed_values.add(name) return object.__setattr__(self, name, value)
def __init__(self, *args, **kw): from motorengine.fields.dynamic_field import DynamicField self._id = kw.pop('_id', None) self._values = {} for key, field in self._fields.items(): if callable(field.default): self._values[field.db_field] = field.default() else: self._values[field.db_field] = field.default for key, value in kw.items(): if key not in self._db_field_map: self._fields[key] = DynamicField(db_field="_%s" % key) self._values[key] = value
def __init__(self, _is_partly_loaded=False, _reference_loaded_fields=None, **kw): """ :param _is_partly_loaded: is a flag that indicates if the document was loaded partly (with `only`, `exlude`, `fields`). Default: False. :param _reference_loaded_fields: dict that contains projections for reference fields if any. Default: None. :param kw: pairs of fields of the document and their values """ for key in DISABLED_FIELDS: kw.pop(key, None) self._fields.pop(key, None) from motorengine.fields.dynamic_field import DynamicField _id = kw.pop('_id', None) self.set_id(_id) self._values = {} self._changed_values = set() self.is_partly_loaded = _is_partly_loaded if _reference_loaded_fields: self._reference_loaded_fields = _reference_loaded_fields else: self._reference_loaded_fields = {} for key, field in list(self._fields.items()): if isinstance(field, DateTimeField) and field.auto_now_on_update: self._changed_values.add(key) if callable(field.default): self._values[field.name] = field.default() else: self._values[field.name] = field.default for key, value in list(kw.items()): if key in self._fields: self._values[key] = value elif self.__allow_undefined_fields__: self._fields[key] = DynamicField(db_field="_%s" % key.lstrip('_')) self._values[key] = value else: pass
class TestDocument(Document): dynamic_field = DynamicField(db_field="_test_something_name")