<https://github.com/aumo/django_multifield_clean>
Provides a Django form mixin that allows declaring validation methods
(similar to the Django `Form.clean_<field_name>()` methods) working on multiple
fields, allowing less boilerplate code in `Form.clean()`.
*Tested on Django 1.7, 1.8 and Python 2.7, 3.2, 3.3, 3.4*
## Installation
`pip install django_multifield_clean`
## Usage
Make your form inherit from the `multifieldclean.forms.MultiFieldCleanFormMixin` mixin. Note that the mixin must come **before** the Django form class.
Declare methods starting with `MULTIFIELD_CLEAN_METHOD_PREFIX` (default is `multi_clean`) which arguments are named after field names.
Those methods will be called during the form validation process, after `Form.clean()`, with the field values their arguments are named after as parameters.
If an argument has no default value, the validation method is called only if the field it is refering to has a value.
Example:
from django import forms
from django.core.exceptions import Validationerror
from multifieldclean.forms import MultiFieldCleanFormMixin
# The mixin must be placed before 'forms.Form'
# so it can override its methods.
class HolidaysForm(MultiFieldCleanFormMixin, forms.Form):
start = forms.IntegerField()
end = forms.IntegerField()
# Any method starting with "multiclean" will be
# interpreted as a multi field validation method.
# The method will only be called only if 'start' and
# 'end' have values.
def multiclean_holidays_end_after_they_start(self, start, end):
if start > end:
self.add_error('end',
ValidationError('These holidays end before they even start!'))
### Configuration
* `MULTIFIELD_CLEAN_METHOD_PREFIX`: modify the prefix used to detect validation methods, default is `multiclean`.
## Why?
I often found myself writing a lot of boilerplate code when overriding the `Form.clean()` method, for example:
def clean(self):
start = self.cleaned_data.get('start')
end = self.cleaned_data.get('end')
if start and end:
if start > end:
# etc...
It is boring to write and harder to read. This package tries to provide a solution to this issue.
## Contributing
Pull requests are welcome.
Runnig the tests:
* To run the full suite, use `tox`.
* To run them for your current environment:
* Install test requirements `pip install -r test-requirements.txt`
* Install the targeted Django version.
* Run `py.test`
* Use `py.test --cov multifieldclean` to get the coverage report.
Metadata
Release files for django_multifield_clean 0.0.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 | Provenance | Actions |
|---|---|---|---|---|
| django_multifield_clean-0.0.1.tar.gz | 4.9 kB | Without provenance | Details for django_multifield_clean-0.0.1.tar.gz |
Release files / django_multifield_clean-0.0.1.tar.gz
| Download URL | django_multifield_clean-0.0.1.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ba818f3ce5786108a162beb15d15c9844925132111f3af3c10819d283a74f426
|
|
BLAKE2b-256 checksum How to use checksums |
5d3a3cf027bb6d24234ffbb247085da6241d0417154ea2ce760f2aebf2d8fe07
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for django_multifield_clean 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.