Django Form'initial'和'bound data'之间的区别?

时间:2021-05-31 19:23:24

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.

文档似乎建议“初始是动态初始值”,但是能够将“绑定数据”传递给构造函数完成完全相同的事情。我曾经使用过去的初始数据来表示动态值,但我很想使用更简单的“绑定数据”样式,但是想要了解这两种样式之间真正区别的一些见解。

3 个解决方案

#1


36  

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

这是关于绑定和未绑定表单的django文档的关键部分。

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

Form实例绑定到一组数据或未绑定:

  • 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.
  • 如果它绑定到一组数据,它就能够验证该数据并将表单呈现为HTML,并在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.
  • 如果它是未绑定的,则无法进行验证(因为没有要验证的数据!),但它仍然可以将空白表单呈现为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.

此表单无效,因为未指定年龄。在模板中呈现表单时,您将看到age字段的验证错误。

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


7  

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


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

另一个区别是数据需要小部件可以解析的东西,而初始是每个字段。如果你这样做会有所不同使用MultiWidgets。在这种情况下,数据应包含类似的内容

{'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'}

#1


36  

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

这是关于绑定和未绑定表单的django文档的关键部分。

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

Form实例绑定到一组数据或未绑定:

  • 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.
  • 如果它绑定到一组数据,它就能够验证该数据并将表单呈现为HTML,并在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.
  • 如果它是未绑定的,则无法进行验证(因为没有要验证的数据!),但它仍然可以将空白表单呈现为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.

此表单无效,因为未指定年龄。在模板中呈现表单时,您将看到age字段的验证错误。

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


7  

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


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

另一个区别是数据需要小部件可以解析的东西,而初始是每个字段。如果你这样做会有所不同使用MultiWidgets。在这种情况下,数据应包含类似的内容

{'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'}