初始数据和绑定数据有什么区别? (Django 形式)
给出这样一个例子:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 django 文档中 bound 的关键部分和未绑定的表单。
您无法真正看到您提供的示例表单的差异,因为该表单在“绑定数据”样式中有效。我们来扩展一下表单,添加一个
age
字段,这样区别会更加明显。绑定表单
此表单无效,因为未指定
age
。当您在模板中呈现表单时,您将看到age
字段的验证错误。具有动态初始数据的未绑定表单
此表单未绑定。不会触发验证,因此渲染模板时不会显示任何错误。
Here's the key part from the django docs on bound and unbound forms.
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.Bound form
This form is invalid, because
age
is not specified. When you render the form in the template, you will see validation errors for theage
field.Unbound form with dynamic initial data
This form is unbound. Validation is not triggered, so there will not be any errors displayed when you render the template.
不,这不是区别所在(我很想知道您在文档中的哪里得到了这种印象)。区别在于是否执行验证。
初始数据不会触发验证。例如,这允许您预先填写某些字段,但将其他字段留空,即使它们是必需的。如果您使用绑定数据,即使在第一次查看该表单时,您也会因那些空的必填字段而收到错误,这对用户来说会很烦人。
当然,绑定数据会触发验证。此外,如果您使用模型表单,相关实例将仅使用绑定数据更新,而不是初始数据。
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.
另一个区别是
data
需要小部件可以解析的内容,而initial
是每个字段的。例如,如果您使用MultiWidget
,这会产生影响。在这种情况下,data
应该包含类似的内容,而
initial
则需要这样的内容:Another difference is that
data
expects something that widgets can parse whereasinitial
is per-field. This makes a difference if you e.g. useMultiWidget
s. In such casedata
should contain something likewhereas
initial
expects something like this: