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

☆樱花仙子☆ 提交于 2019-12-17 17:54:04

问题


Given an example like this:

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

I'm trying to grasp what the difference between the following two snippets is:

"Bound Data" style:

my_form = MyForm({'name': request.user.first_name})

"Initial data" style:

my_form = MyForm(initial={'name': request.user.first_name})

The documentation seems to suggest than "initial is for dynamic initial values", and yet being able to pass "bound data" to the constructor accomplishes exactly the same thing. I've used initial data in the past for dynamic values, but I'm tempted to use the more straightforward "bound data" style, but would like some insights about what the real difference between these two styles is.


回答1:


Here's the key part from the django docs on bound and unbound forms.

A Form instance is either bound to a set of data, or unbound:

  • If it’s bound to a set of data, it’s capable of validating that data and rendering the form as HTML with the data displayed in the HTML.
  • If it’s unbound, it cannot do validation (because there’s no data to validate!), but it can still render the blank form as HTML.

You can't really see the difference for the example form you gave, because the form is valid in the "bound data" style. Let's extend the form by adding an age field, then the difference will be more obvious.

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

Bound form

my_form = MyForm({'name': request.user.first_name})

This form is invalid, because age is not specified. When you render the form in the template, you will see validation errors for the age field.

Unbound form with dynamic initial data

my_form = MyForm(initial={'name':request.user.first_name})

This form is unbound. Validation is not triggered, so there will not be any errors displayed when you render the template.




回答2:


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.




回答3:


Another difference is that data expects something that widgets can parse whereas initial is per-field. This makes a difference if you e.g. use MultiWidgets. In such case data should contain something like

{'myfield_0': 'data for subwidget 0', 
 'myfield_1': 'data for subwidget 1'}

whereas initial expects something like this:

{'myfield': 'data for subwidget 0,data for subwidget 1'}


来源:https://stackoverflow.com/questions/7920128/difference-between-django-form-initial-and-bound-data

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