If you look at the example here,
>>> for form in formset:
... print form.as_table()
<tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" value="Django is now open source" id="id_form-0-title" /></td></tr>
<tr><th><label for="id_form-0-pub_date">Pub date:</label></th><td><input type="text" name="form-0-pub_date" value="2008-05-12" id="id_form-0-pub_date" /></td></tr>
<tr><th><label for="id_form-1-title">Title:</label></th><td><input type="text" name="form-1-title" id="id_form-1-title" /></td></tr>
<tr><th><开发者_StackOverflowlabel for="id_form-1-pub_date">Pub date:</label></th><td><input type="text" name="form-1-pub_date" id="id_form-1-pub_date" /></td></tr>
<tr><th><label for="id_form-2-title">Title:</label></th><td><input type="text" name="form-2-title" id="id_form-2-title" /></td></tr>
<tr><th><label for="id_form-2-pub_date">Pub date:</label></th><td><input type="text" name="form-2-pub_date" id="id_form-2-pub_date" /></td></tr>
You'll see the first form is filled out, the 2nd 2 aren't. I want to render that filled out form as hidden, and the other 2 as visible. How do I distinguish between the two?
I think form.is_valid
will return true for blank forms in a formset, no?
Why do you want to render the existing models at all if they are just going to be hidden?
Something like the following will just give you a set of empty forms, excluding any existing objects:
formset = MyFormset(queryset=MyModel.objects.none())
Nevermind... solution is actually pretty simple.
{{vehicle_formset.management_form}}
{% if vehicle_formset.initial_forms %}
{% for vehicle_form in vehicle_formset.initial_forms %}
{% for field in vehicle_form %}{{ field.as_hidden}}{% endfor %}
{% endfor %}
{% endif %}
{% for vehicle_form in vehicle_formset.extra_forms %}
<!-- render non-hidden forms -->
{% endfor %}
精彩评论