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 ?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With