Mixins for model cleanup methods and validation error concatenations
Free software: MIT license
Documentation: https://django-model-cleanup.readthedocs.io.
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
Quickstart
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 = []
try:
self.clean_foo()
except ValidationError as ex:
errors.append(ex)
errors = []
try:
self.clean_bar()
except ValidationError as ex:
errors.append(ex)
errors = []
try:
self.clean_legacy()
except ValidationError as ex:
errors.append(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.
Credits
This package was created with Cookiecutter and the wooyek/cookiecutter-django-app project template.
History
0.1.0 (2017-12-05)
First release on PyPI.
Metadata
Release files for django-model-cleanup 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-model-cleanup-0.1.1.tar.gz | 30.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_model_cleanup-0.1.1-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 38.7 kB
Release files / django-model-cleanup-0.1.1.tar.gz
| Download URL | django-model-cleanup-0.1.1.tar.gz |
|---|---|
| Size | 30.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0fd7a92543e69e1d6007c064855e5705220409f74582859332639f268eb55e2a
|
|
BLAKE2b-256 checksum How to use checksums |
6b13b49e114de028082ee14db160e46f7db0ee52d0da1f730b0b9bd3c0d121a2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / django_model_cleanup-0.1.1-py2.py3-none-any.whl
| Download URL | django_model_cleanup-0.1.1-py2.py3-none-any.whl |
|---|---|
| Size | 8.6 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
788909682e4a572059f8b568eb3ddd1d4bababa18c750637d3cb8de8b1453166
|
|
BLAKE2b-256 checksum How to use checksums |
f02b44efc0669610cb3b5613075501dd587ee0f58ff044baeb00455cf8ce4062
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |