django_sorcery.db.models module

sqlalchemy model related things.

class django_sorcery.db.models.Base[source]

Bases: django_sorcery.db.mixins.CleanMixin

Base model class for SQLAlchemy.

Can be overwritten by subclasses:

query_class = None

Automatically added by declarative base for easier querying:

query = None objects = None
as_dict()[source]

Return a dict of column attributes.

class django_sorcery.db.models.BaseMeta(classname, bases, dict_, **kw)[source]

Bases: sqlalchemy.orm.decl_api.DeclarativeMeta

Base metaclass for models which registers models to DB model registry when models are created.

django_sorcery.db.models.autocoerce(cls)[source]

This function automatically registers attribute events that coerces types for the attribute using django’s form fields for a given model classs. If no class is provided, it will wire up coersion for all mappers so it can be used as a class decorator or globally.

@autocoerce_properties
class MyModel(db.Model):
    ...

or:

class MyModel(db.Model):
    ...

autocoerce_properties()

Since django form fields are used for coersion, localization settings such as USE_THOUSAND_SEPARATOR, DATE_INPUT_FORMATS and DATETIME_INPUT_FORMATS control type conversions.

django_sorcery.db.models.autocoerce_properties(*attrs)[source]

This function automatically registers attribute events that coerces types for given attributes using django’s form fields.

::
class MyModel(db.Model):
field1 = Column(…) field2 = Column(…) field3 = Column(…) …

autocoerce_properties(MyModel.field1, MyModel.field2) # Will only force autocoersion on field1 and field2

django_sorcery.db.models.clone(instance, *rels, **kwargs)[source]
instance: Model
a model instance
relations: list or relations or a tuple of relation and kwargs for that relation
relationships to be cloned with relationship and optionally kwargs
kwargs: dict string of any
attribute values to be overridden
django_sorcery.db.models.deserialize(model, data)[source]
model:
a model class
data: dict
values
django_sorcery.db.models.full_clean_flush_handler(session, **kwargs)[source]

Signal handler for executing full_clean on all dirty and new objects in session.

django_sorcery.db.models.instant_defaults(cls)[source]

This function automatically registers attribute events that sets the column defaults to a model instance at model instance initialization provided that default values are simple types:

@instant_defaults
class MyModel(db.Model):
    attr = db.Column(..., default=1)

assert MyModel().default == 1
django_sorcery.db.models.serialize(instance, *rels)[source]
instance:
a model instance
rels: list of relations
relationships to be serialized
django_sorcery.db.models.simple_repr(instance, fields=None)[source]
instance: Model
a model instance
fields: list
list of fields to display on repr