Skip to main content

Mixins for model cleanup methods and validation error concatenations

Project description Documentation Status coverage CodeCov coverage Maintainability License Tweet about this project!-1EAEDB.svg

Mixins for model cleanup methods and validation error concatenations

Features of CleanMixin

  • Provides clean method implementation

  • Call to full_clean will result in call to all clean_* methods

  • All methods will get called regardless of validation errors - get all errors at once

  • Auto mapping of errors to field names based on clean method names, if errors have no error_dict


Install Django Model Cleanup:

pip install django-model-cleanup

Add mixin in your models and enjoy clean_ method collection and error concatenation when full_clean is called:

from django.core.exceptions import ValidationError
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django_model_cleanup import CleanMixin, ExtensibleValidationError

class SomeModel(CleanMixin, models.Model):
    lorem = models.CharField(max_length=10, blank=True)

    def clean_foo(self):
        raise ValidationError('Foo is bad')

    def clean_bar(self):
        raise ExtensibleValidationError({'bar': _('Bar is wrong cause %s > %s!')}, code='bar', params=(2, 1))

    def clean_legacy(self):
        # We can't init ValidationError as one-liner, cause dict + params are not compatible
        # We need to wrap a message in ValidationError and put that in dict indicating a field
        msg = _('Bar legacy error %s > %s!')
        error = ValidationError(msg, code='bar', params=(7, 5))
        raise ValidationError({'bar': error})

Each error handling and concatenation is no longer required:

# This is not longer required:
def clean(self):
    errors = []
    except ValidationError as ex:
    errors = []
    except ValidationError as ex:
    errors = []
    except ValidationError as ex:
    if errors:
        raise ValidationError(errors)

Running Tests

Does the code actually work?


$ pipenv install –dev $ pipenv shell $ tox

We recommend using pipenv but a legacy approach to creating virtualenv and installing requirements should also work. Please install requirements/development.txt to setup virtual env for testing and development.


This package was created with Cookiecutter and the wooyek/cookiecutter-django-app project template.


0.1.0 (2017-12-05)

  • First release on PyPI.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-model-cleanup-0.1.1.tar.gz (30.1 kB view hashes)

Uploaded source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page