- Author:
- dcwatson
- Posted:
- September 13, 2012
- Language:
- Python
- Version:
- Not specified
- Score:
- 1 (after 1 ratings)
This method will return an inline formset class that validates values across the given field are unique among all forms. For instance:
ApprovedUserFormSet = inlineformset_factory(Request, ApprovedUser, formset=unique_field_formset('email'), form=ApprovedUserForm)
Will make sure all ApprovedUser objects created for the Request have unique "email" fields.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | def unique_field_formset(field_name): from django.forms.models import BaseInlineFormSet class UniqueFieldFormSet (BaseInlineFormSet): def clean(self): if any(self.errors): # Don't bother validating the formset unless each form is valid on its own return values = set() for form in self.forms: value = form.cleaned_data[field_name] if value in values: raise forms.ValidationError('Duplicate values for "%s" are not allowed.' % field_name) values.add(value) return UniqueFieldFormSet |
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
If the errors are not visible in the view, you are probably making the same mistake I made. To display the validation errors in the view, you must add {{ formset.non_form_errors }} to it.
#
A minor change
def unique_field_formset(field_name): from django.forms.models import BaseInlineFormSet class UniqueFieldFormSet(BaseInlineFormSet): def clean(self): if any(self.errors): # Don't bother validating the formset unless each form is valid on its own return values = set() for form in self.forms: if form.cleaned_data: value = form.cleaned_data[field_name] if value in values: raise forms.ValidationError('Duplicate values for "%s" are not allowed.' % field_name) values.add(value) return UniqueFieldFormSet
#
I made these adjustments:
from django.utils.translation import gettext_lazy as _
def unique_field_formset(field_name): from django.forms.models import BaseInlineFormSet
#
Please login first before commenting.