Unique field inline formset

 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

  1. InlineFormset Template by petry 4 years, 6 months ago
  2. Arbitrary length formset by Rupe 3 years, 8 months ago
  3. Printing inline formsets as UL / P by ikke 3 years, 9 months ago
  4. Add validation for 'unique' and 'unique_together' constraints to newforms created dynamically via form_for_model or form_for_instance by bikeshedder 5 years, 11 months ago
  5. Handles Inline Formsets and also "in-standard-way" normal forms by sebnapi 1 year, 2 months ago

Comments

(Forgotten your password?)