Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django modelformset exclude ID field

I have a modelformset that I've created in a view as such:

    CarpoolFamilyInviteModelFormset = modelformset_factory(CarpoolFamilyInvite, fields=('family_name','family_email'), extra=3)

As you can see, I've tried to limit it to two fields: family_name and family_email. However, when I render it in the template, I get an extra field, ID. Here's the template code:

            {% for form in formset %}
                <div class="row">
                {% for field in form %}
                    <div class="span3">
                        <input id="focusedInput" class="input-large focused" type="text" placeholder="{{ field.label }}">
                    </div>
                {% endfor %}
                </div>
            {% endfor %}

and here's what the form looks like:

enter image description here

Any idea how I can skip showing the ID fields? exclude="ID" doesn't seem to have any effect. THanks!

like image 270
mb52089 Avatar asked Sep 15 '25 00:09

mb52089


1 Answers

The model formset relies on the id field, so it is not possible to exclude it.

The best option is to render the id field as a hidden field. See the docs for looping over hidden and visible fields for an example.

like image 101
Alasdair Avatar answered Sep 17 '25 02:09

Alasdair