Package: django-superform
django-superformLess sucking formsets.ASuperFormis absolutely super if you want to nest a lot of forms in each
other. Use formsets and nested forms inside theSuperForm. TheSuperFormwill take care of its children!NoteThis package is still in rapid development. Some APIs might change in the
future and it’s not yet feature complete. The documentation is not yet
complete either, but everything that is documented, is up to date and
should work as stated. If not, then please file a bug.
Every non-backwards compatible change and new features will be documented
in thechangelog.Imagine you want to have a view that shows and validates a form and a formset.
Let’s say you have a signup form where users can enter multiple email
addresses. Django providesformsetsfor this usecase, but handling those in a
view is usually quite troublesome. You need to validate both the form and the
formset manually and you cannot use django’s genericFormView. So here comesdjango-superforminto play.Here we have an example for the usecase. Let’s have a look at theforms.py:fromdjangoimportformsfromdjango_superformimportSuperModelForm,InlineFormSetFieldfrommyapp.modelsimportAccount,EmailclassEmailForm(forms.ModelForm):classMeta:model=Emailfields=('account','email',)EmailFormSet=modelformset_factory(EmailForm)classSignupForm(SuperModelForm):username=forms.CharField()# The model `Email` has a ForeignKey called `user` to `Account`.emails=InlineFormSetField(formset_class=EmailFormSet)classMeta:model=Accountfields=('username',)So we assign theEmailFormSetas a field directly to theSignupForm.
That’s where it belongs! Ok and how do I handle this composite form in the
view? Have a look:defpost_form(request):ifrequest.method=='POST':form=PostForm(request.POST)ifform.is_valid():account=form.save()returnHttpResponseRedirect('/success/')else:form=PostForm()returnrender_to_response('post_form.html',{'form',},context_instance=RequestContext(request))No, we don’t do anything different as we would do without having theFormSeton theSignupForm. That way you are free to implement all the
logic in the form it self where it belongs and use generic views likeCreateViewyou would use them with simple forms. Want an example for this?fromdjango.views.genericimportCreateViewfrommyapp.modelsimportAccountfrommyapp.formsimportSignupFormclassSignupView(CreateView):model=Accountform_class=SignupFormurlpatterns=patterns('',url('^signup/$',SignupView.as_view()),)And it just works.DocumentationFull documentation is available on Read The Docs:https://django-superform.readthedocs.org/Developed by Gregor Müllegger in cooperation withTeam23.Changelog0.3.1SuperForm.composite_fieldsdict is not longer shared between form
instances. Every new form instances get’s a deep copy. So changes to it
won’t leak into other instances of the same form class.0.3.0#11: FixCompositeBoundFieldto allow direct access to nested form
fields viaform['nested_form']['field'].Support for Django’s Media handling in nested forms. See#3and#5.Do not populate errorlist representations without any errors of nested
formsets into the errors of the super form. See#5for details.0.2.0Django 1.8 support.Initial values given to the__init__method of the super-form will get
passed through to the nested forms.Theempty_permittedargument for modelforms used in aModelFormFieldis set depending on therequiredattribute given to the field.0.1.0Initial release with proof of concept.