- 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
- Mask sensitive data from logger by agusmakmun 1 week, 6 days ago
- Template tag - list punctuation for a list of items by shapiromatron 1 year, 2 months ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 2 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 9 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 10 months ago
Comments
Please login first before commenting.