Login

Must complete at least one form in formset_factory()

Author:
martino
Posted:
June 30, 2010
Language:
Python
Version:
Not specified
Score:
0 (after 0 ratings)

Workaround to stop formset_factory form being submitted completely blank. This will only allow form.is_valid() to return True if the first form has been filled in and validates.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from django import forms

class PersonForm(forms.Form):
  first_name = forms.CharField(max_length=255)
  last_name = forms.CharField(max_length=255)
  email = forms.EmailField(max_length=255)
  
class NoEmptyFormsAllowedBaseFormSet(forms.formsets.BaseFormSet):
  """
  Won't allow formset_factory to be submitted with no forms
  """
  def clean(self):
    if "cleaned_data" in self.forms[0] and not self.forms[0].cleaned_data:
      raise forms.ValidationError, "Please fill in at least one form"



formset = formset_factory(PersonForm, extra=1, formset=NoEmptyFormsAllowedBaseFormSet)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.