Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

empty_form initial value for a modelformset_factory

I try to add initial values to the empty form of a modelformset_factory.

FormSet = modelformset_factory(MyModel, extra=2)
formset = FormSet(queryset=MyModel.objects.none(), initial=[{'foo': 'bar'}, {'foo': 'bar'}])

I would like to set initial value to the formset.empty_form , how can I achieve this ?

like image 615
sam Avatar asked Jul 09 '10 13:07

sam


1 Answers

You can add initial values to your formset as clearly stated in this other answer. Then you're able to set the empty_form in the template such as:

<form action="/contact/" method="post">{% csrf_token %}
   {% with formset.empty_form as form %}
      {% for field in form %}
         <div class="fieldWrapper">
            {{ field.errors }}
            {{ field.label_tag }}: {{ field }}
         </div>
      {% endfor %}
   {% endwith %}
   <input type="submit" value="Submit" />
</form>
like image 130
vascop Avatar answered Oct 21 '22 02:10

vascop