See the form validation for more information on how validators are run
in forms, and Validating objects for how they’re run in models. Note
that validators will not be run automatically when you save a model,
but if you are using a ModelForm, it will run your validators on any
fields that are included in your form. See the ModelForm documentation
for information on how model validation interacts with forms.
使用^{} 。
根据文档,似乎没有办法在字段上设置数据库约束。最好是添加模型“验证器”,如果调用模型验证或使用
ModelForm
,则将调用该验证器。如果只将值放入对象并save()
,则跳过验证程序。因此,您可以在表单上添加验证,也可以在模型中添加验证,如果您使用
ModelForm
,该模型也可以在表单级别运行。从docs on "How validators are run":
你可以这样做:
这也对“price”的验证器值负责。
相关问题 更多 >
编程相关推荐