Djangocon: The whys and hows of using django formsets - Shena Priscilla Makini

Tags: djangocon, django

(One of the summaries of a talk at the 2014 djangocon.eu).

Many people don’t use or know about django formsets.

She google for articles and found “formsets: the worst thing ever” and “formset gotcha: or how to lose your sanity”. Ouch.

What are form sets for? When you want to use the same form multiple times. For instance for a data grid. A table with one form per row, for instance. Django formsets help you put several of those forms on one page.

The example she showed was of changing your “mailman” settings for a couple of mailinglists on one page. It is used in postorius, a new web UI (django) for mailman.

It works something like this:

SomeFormset = formsets.formset_factory(SomeForm, extra=5)
formset = SomeFormset()

And in the template it is just:

{% formset %}

This gives it to you as a table. You could also iterate over it and get the individual forms:

{% for form in formset %}
     ...
{% endfor %}

(Personal note: oh blast, I didn’t know about this feature! I know just the spot where I desperately need to use it. That one app where I do some manual field generation and mess about with the IDs and a custom template. So… thanks for the talk!)

http://reinout.vanrees.org/images/2014/django5.jpg

French “mallet” loco in 2007. Magnificent machine. Vallée de la Doller.

 
vanrees.org logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):