Django Formset 添加两行而不是具有相同输入 ID 的一行

Django Formset adding two rows instead of one with same input id

我正在尝试使用 django 实现动态表单集。它工作正常,只是在单击添加按钮时添加了两行具有相同 ID 的输入字段。应该只添加一行。

models.py

class StaffEmailCC(BaseModel):
    staff_email = models.ForeignKey("projects.StaffEmail",blank=True,null=True)
    cc_addresses=models.TextField(blank=True,null=True)
    class Meta:
        db_table = 'staffmailcc'
        verbose_name = _('staffmailcc')
        verbose_name_plural = _('staffmailcc')
        ordering = ('-date_added',)
    def __unicode__(self):
        return self.cc_addresses

forms.py

class StaffEmailCCForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(StaffEmailCCForm, self).__init__(*args, **kwargs)
    class Meta:
        model = StaffEmailCC
        fields = ['cc_addresses']
        widgets = {
            'cc_addresses': autocomplete_light.ChoiceWidget('StaffAutocomplete'),
        }

views.py

def create_staff_mail(request):
    StaffEmailCCFormset = formset_factory(StaffEmailCCForm)
    staff_email_cc_formset = StaffEmailCCFormset(prefix='staff_email_cc_formset')
    form = StaffEmailForm()
    context = {
        "form": form,
        "staff_email_cc_formset":staff_email_cc_formset,
        "title": "Create Staff Mail",
        "sender_email":sender_email,
            }
    return render(request, 'projects/create_staff_mail.html', context)

HTML

<div class="content formElements add_item_container staff_email_cc_formset">
            <table>
                <tr>
                    <th>CC</th>
                </tr>
                {% for i in staff_email_cc_formset.forms %}
                <tr class="form_set_row">
                        <td>
                            <span class="left cc_addresses">
                                {{i.cc_addresses}}
                            </span>
                        </td>
                </tr>
                  {% endfor %}
             </table>
        {{ staff_email_cc_formset.management_form }}
    </div>

js

$('.staff_email_cc_formset table tr.form_set_row').formset({
                prefix: '{{ staff_email_cc_formset.prefix }}',
                formCssClass: 'dynamic-formset1'
            }); 

每次单击添加按钮时,都会添加两行,其中只有一行可以正确使用自动完成功能。知道我做错了什么吗?

Django 表单集默认有 extra=1

[...]The number of empty forms that is displayed is controlled by the extra parameter. By default, formset_factory() defines one extra form[...]

Documentation:在页面中搜索extra=,快速找到您要找的内容。

StaffEmailCCFormset = formset_factory(StaffEmailCCForm, extra=0)

您可以将字段分配给表单集,

如果你这样写formset,它需要额外的字段默认值1:

StaffEmailCCFormset = formset_factory(StaffEmailCCForm)

你覆盖了额外的字段 StaffEmailCCFormset = formset_factory(StaffEmailCCForm, extra=1)

你可以看到formset.py的源代码 https://github.com/django/django/blob/master/django/forms/formsets.py

这可能看起来很奇怪。但是把 js 函数放在 $(document).ready(function (){});解决了我的问题

$(document).ready(function () {
        $('.staff_email_cc_formset table tr.form_set_row').formset({
            prefix: '{{ staff_email_cc_formset.prefix }}',
            formCssClass: 'dynamic-formset1'
        });
    });