初始数据和绑定数据有什么区别? (Django 形式)

发布于 2024-12-12 04:15:21 字数 442 浏览 0 评论 0原文

给出这样一个例子:

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

我试图理解以下两个片段之间的区别:

“绑定数据”样式:

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

“初始数据”样式:

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

文档似乎建议“初始用于动态初始值”,并且然而,能够将“绑定数据”传递给构造函数可以完成完全相同的事情。我过去曾使用初始数据作为动态值,但我很想使用更直接的“绑定数据”样式,但希望了解这两种样式之间的真正区别是什么。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

超可爱的懒熊 2024-12-19 04:15:21

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

表单实例要么绑定到一组数据,要么未绑定

  • 如果它绑定到一组数据,它就能够验证该数据并将表单呈现为 HTML,并在 HTML 中显示数据。
  • 如果未绑定,则无法进行验证(因为没有数据可供验证!),但它仍然可以将空白表单呈现为 HTML。

您无法真正看到您提供的示例表单的差异,因为该表单在“绑定数据”样式中有效。我们来扩展一下表单,添加一个age字段,这样区别会更加明显。

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

绑定表单

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

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

具有动态初始数据的未绑定表单

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

此表单未绑定。不会触发验证,因此渲染模板时不会显示任何错误。

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.

狠疯拽 2024-12-19 04:15:21

不,这不是区别所在(我很想知道您在文档中的哪里得到了这种印象)。区别在于是否执行验证。

初始数据不会触发验证。例如,这允许您预先填写某些字段,但将其他字段留空,即使它们是必需的。如果您使用绑定数据,即使在第一次查看该表单时,您也会因那些空的必填字段而收到错误,这对用户来说会很烦人。

当然,绑定数据会触发验证。此外,如果您使用模型表单,相关实例将仅使用绑定数据更新,而不是初始数据。

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.

深空失忆 2024-12-19 04:15:21

另一个区别是 data 需要小部件可以解析的内容,而 initial 是每个字段的。例如,如果您使用MultiWidget,这会产生影响。在这种情况下,data 应该包含类似的内容

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

,而 initial 则需要这样的内容:

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

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'}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文