def __init__(self, hash_key=False, range_key=False, index=None, data_type=NO_ARG, type=six.text_type, coerce=False, check=None, nullable=True, default=NO_ARG, **metadata): if hash_key and range_key: raise ValueError("hash_key and range_key are mutually exclusive!") self.name = None self.model = None self.composite = False if data_type is NO_ARG: data_type = type if isinstance(data_type, TypeDefinition): self.data_type = data_type elif (inspect.isclass(data_type) and issubclass(data_type, TypeDefinition)): self.data_type = data_type() else: type_factory = ALL_TYPES.get(data_type) if type_factory is None: raise TypeError("Unrecognized data_type '%s'" % data_type) self.data_type = type_factory() self._coerce = coerce self.check = [] if check is not None: if hasattr(check, '__iter__'): self.check = list(check) else: self.check = [check] self.nullable = nullable if not self.nullable: self.check.append(lambda x: not is_null(x)) self.hash_key = hash_key self.range_key = range_key self.subfields = [] self.index = False self.index_name = None self._ddb_index = None self._ddb_index_kwargs = None if default is NO_ARG: if self.is_set: self._default = set() else: self._default = None else: self._default = default if index: self.all_index(index) self.metadata = metadata
def __new__(cls, *_, **__): """ Override __new__ to set default field values """ obj = super(Model, cls).__new__(cls) mark_dirty = [] with obj.loading_(): for name, field in six.iteritems(cls.meta_.fields): if not field.composite: setattr(obj, name, field.default) if not is_null(field.default): mark_dirty.append(name) obj.__dirty__.update(mark_dirty) obj._persisted = False return obj
def __init__(self, hash_key=False, range_key=False, index=None, data_type=NO_ARG, type=six.text_type, coerce=False, check=None, nullable=True, default=NO_ARG): if hash_key and range_key: raise ValueError("hash_key and range_key are mutually exclusive!") self.name = None self.model = None self.composite = False if data_type is NO_ARG: data_type = type if isinstance(data_type, TypeDefinition): self.data_type = data_type elif (inspect.isclass(data_type) and issubclass(data_type, TypeDefinition)): self.data_type = data_type() else: type_factory = ALL_TYPES.get(data_type) if type_factory is None: raise TypeError("Unrecognized data_type '%s'" % data_type) self.data_type = type_factory() self._coerce = coerce self.check = [] if check is not None: if hasattr(check, '__iter__'): self.check = list(check) else: self.check = [check] if not nullable: self.check.append(lambda x: not is_null(x)) self.hash_key = hash_key self.range_key = range_key self.subfields = [] self.index = False self.index_name = None self._ddb_index = None self._ddb_index_kwargs = None if default is NO_ARG: if self.is_set: self._default = set() else: self._default = None else: self._default = default if index: self.all_index(index)