Model Formset - By Default model formset is rendering one extra field (2 fields in total)

随声附和 提交于 2020-01-01 14:45:13

问题


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:


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.



来源:https://stackoverflow.com/questions/31774076/model-formset-by-default-model-formset-is-rendering-one-extra-field-2-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!