[Answered ]-How to label a form instance within a django inline formset?

2👍

First, note that an inline formset is a small wrapper around a model formset, so most of what applies to the latter applies to the former. And a model formset is a way to present a set of forms each one being related to an instance of a model. So you are trying to think about several levels of abstraction all at once.

Next, see the documentation for “Using a formset in views and templates” which describes writing your own template to render the formset:

<form method="post" action="">
    {{ formset.management_form }}
    <table>
        {% for form in formset %}
        {{ form }}
        {% endfor %}
    </table>
</form>

So if you want to label each form – remembering that each one is itself a ModelForm describing one instance of the model – you have access to that form within the {% for %} block:

<form method="post" action="">
    {{ formset.management_form }}
    <table>
        {% for form in formset %}
        <tr><th>{{ form.instance.foo_field }}</th></tr>
        {{ form }}
        {% endfor %}
    </table>
</form>

Leave a comment