Wednesday, 12 August 2009

Requiring at least one inline FormSet

Last month I posted an article about my Improved Django FormWizard, well this month I've release a simple subclass of Django's BaseInlineFormSet that demonstrates how you can require a user to enter at least one entry in an inline formset.

After updating to wadofstuff.django.forms 1.1.0 you can use the RequireOneFormSet class as the formset argument to inlineformset_factory().

When the formset is validated and it does not contain one or more entries, then a ValidationError is raised which gets put into formset.non_form_errors. You will need to check this in your templates if you wish to display the error message to your users.


dingue said...


I know that this might be a stupid question but is this meant to be used with the Improved FormWizard? If so how would you go about implementing it?

matthew said...


Sorry RequireOneFormSet can't be used with BoundFormWizard as it subclasses BaseInlineFormSet and the wizard classes don't support inline formsets. You could try reimplementing it as a subclass of BaseFormSet then you would pass the new class you've written as the formset argument to formset_factory().

mrts said...

Thanks! Simple and clean.

phabrik said...

that´s not working of you delete all rows within a formset, because the clean-method is not called.