django_sorcery.db.mixins module

Common mixins used in models.

class django_sorcery.db.mixins.CleanMixin[source]

Bases: object

Mixin for adding django-style full_clean validation to any object.

Base model in sqlalchemy.SQLAlchemy already uses this mixin applied.

For example:

class Address(db.Model):
    city = db.Column(db.String(20))
    state = db.Column(db.String(2))
    date = db.Column(db.Date())

    validators = [
        ValidateTogetherModelFields(["city", "state"]),
    ]

    def clean_date(self):
        if self.date > datetime.date.today():
            raise ValidationError("Cant pick future date")

    def clean(self):
        if self.date.year < 1776 and self.state == "NY":
            raise ValidationError("NY state did not exist before 1776")
clean(**kwargs)[source]

Hook for adding custom model validations before model is flushed.

Should raise ValidationError if any errors are found.

clean_fields(exclude=None, **kwargs)[source]

Clean all fields on object.

clean_nested_fields(exclude=None, **kwargs)[source]

Clean all nested fields which includes composites.

clean_relation_fields(exclude, **kwargs)[source]

Clean all relation fields.

full_clean(exclude=None, **kwargs)[source]

Run model’s full clean chain.

This will run all of these in this order:

  • will validate all columns by using clean_<column> methods
  • will validate all nested objects (e.g. composites) with full_clean
  • will run through all registered validators on validators attribute
  • will run full model validation with self.clean()
  • if recursive kwarg is provided, will recursively clean all relations. Useful when all models need to be explicitly cleaned without flushing to DB.
run_validators(**kwargs)[source]

Check all model validators registered on validators attribute.