Django Formset.is_valid() failing for extra forms

我是研究僧i 提交于 2019-11-30 06:44:31

Thanks Carl, you led me to discover the root of my problem.

When creating a form with a choice field, which is required, we must set an initial value, otherwise the form will consider that field changed.

So for a form like this:

class SomeForm(forms.Form):

    A = 0
    B = 1
    C = 2
    D = 3

   choices = ((A, 'Aah'), (B, 'Baa'), (C, 'Caa'), (D, 'Daa'))

    # This is a required choice field
    pickme = forms.ChoiceField(choices=choices)

we do this:

pickme = forms.ChoiceField(choices=choices, initial=A)

Then when a formset checks the extra form it will see that pickme had an initial value of A, and it is A now as well, and will consider it unchanged.

Carl Meyer

This is not the usual behavior of formsets. Formsets pass empty_permitted=True to all "extra" forms, and a form with empty_permitted that hasn't been modified should always pass validation. Note that this works just fine in the Django admin (if you use inlines).

You must be doing something else in your code that is breaking this behavior somewhere. Post the full code of the relevant form?

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