Difference between Django Form 'initial' and 'bound data'?

后端 未结 3 375
情话喂你
情话喂你 2020-12-08 08:05

Given an example like this:

class MyForm(forms.Form): 
    name = forms.CharField()

I\'m trying to grasp what the difference between the fo

3条回答
  •  Happy的楠姐
    2020-12-08 08:57

    No, that's not what the difference is (and I'd be interested to know where in the documentation you got that impression from). The difference is whether validation is performed.

    Initial data does not trigger validation. This allows you, for example, to pre-fill certain fields but leave others empty, even though they are required. If you used bound data, you would get errors for those empty required fields even on the first viewing of that form, which would be annoying for the user.

    Bound data, of course, triggers validation. Also, if you're using a modelform, the related instance will only be updated with bound data, not initial data.

提交回复
热议问题