Model Formset - By Default model formset is render

2019-04-09 16:21发布

My model formset without even defining "extra" parameters in modelformset_factory is rendering one extra field in the template. I have tried many variations but it didn't work. If I print the form (the model form) on the command line it just prints a single form field as required but on model formset it prints 2 by default.

Here is my code.

models.py

class Direction(models.Model):
    text = models.TextField(blank=True, verbose_name='Direction|text')

forms.py

class DirectionForm(forms.ModelForm):
    class Meta:
        model = Direction
        fields = ['text',]

views.py

def myview(request):
    Dirset = modelformset_factory(Direction, form=DirectionForm)
    if request.method == "POST":
        dir_formset = Dirset(request.POST or None)
        if dir_formset.is_valid():
        for direction in dir_formset:
            text = direction.cleaned_data.get('text')
            Direction.objects.create(text=text)
return render(request, "test/test.html", {'DirFormSet':Dirset})     

template

{% block content %}
<form method="POST">{% csrf_token %}
<div id="forms">
    {{DirFormSet.management_form}}
    {% for form in DirFormSet %}
        {{form.text}}
        {% if error in form.text.errors %}
            {{error|escape}
        {% endif %}
    {% endfor %}
</div>
<button id="add-another">add another</button>

<input type="submit" />
</form>

{% endblock %}

As a side note, if I submit data on this form it gives the following error. Error

Exception Type: MultiValueDictKeyError
Exception Value:"u'form-0-id'"

1条回答
别忘想泡老子
2楼-- · 2019-04-09 16:45

By default, modelformset_factory creates one extra form. If you don't want any extra forms, set extra=0.

Dirset = modelformset_factory(Direction, form=DirectionForm, extra=0)

The KeyError is because you have not included the form's id field in your template. You should have something like:

{% for form in dir_formset %}
    {{ form.id }}
    {{ form.text }}
    ...
{% endfor %}

Note that you should be passing the formset instance dir_formset when rendering the template, not the class DirFormSet. Your view should be something like

return render(request, "test/test.html", {'dir_formset': dir_formset})     

then the template should be updated to use dir_formset instead of DirFormSet.

查看更多
登录 后发表回答