Django formsets: make first required?

Posted by Mark on Stack Overflow See other posts from Stack Overflow or by Mark
Published on 2010-03-09T03:52:21Z Indexed on 2010/03/09 5:06 UTC
Read the original article Hit count: 419

Filed under:
|

These formsets are exhibiting exactly the opposite behavior that I want.

My view is set up like this:

def post(request): # TODO: handle vehicle formset
    VehicleFormSetFactory = formset_factory(VehicleForm, extra=1)
    if request.POST:
        vehicles_formset = VehicleFormSetFactory(request.POST)
    else:
        vehicles_formset = VehicleFormSetFactory()

And my template looks like this:

    <div id="vehicle_forms">
        {{ vehicles_formset.management_form }}
        {% for form in vehicles_formset.forms %}
            <h4>Vehicle {{forloop.counter}}</h4>
            <table>
                {% include "form.html" %}
            </table>
        {% endfor %}
    </div>

That way it initially generates only 1 form, like I want. But I want that one form to be required!

When I dynamically add blank forms with JavaScript and vehicles_formset.empty_form all those extra forms are required, which I don't want.

From the docs:

The formset is smart enough to ignore extra forms that were not changed.

This is the behavior the first form is exhibiting (not what I want) but not the behavior that the extra forms are exhibiting (what I do want).

Is there some attribute I can can change to at least make one form required?

© Stack Overflow or respective owner

Related posts about django

Related posts about django-formsets