帮助验证模型和表单
我有一些关于模型和表单验证的问题。您能帮我解决这些问题吗:
应该在哪里进行验证?它应该在模型中还是表格中?解决这个问题的正确方法是在表单中使用验证器并在模式中使用约束吗?
在表单中编写“clean_”方法和编写验证器有什么区别?我发现人们经常在“clean_”方法中进行验证检查。
在我正在处理的请求中,URL 字符串中有一个名为“alive”的参数。这通常是 1 或 0。在我的表单中定义它的正确方法是什么?我需要验证它是一个数字,并且只能是 1 或 0。这是正确的方法吗?
alive = models.IntegerField(null=False, max_value=1, min_value=0)
如何为此字段定义默认值,即如果未传递此参数,则默认为 0 (False)。
我在客户端没有表格。我正在使用 Django 表单来验证我的 JS POST 请求。
在模型字段之一中,我需要以 1234x4321 格式存储屏幕分辨率。我应该将其声明为 CharField
在模型和表单中添加一些正则表达式验证吗?任何正则表达式验证的示例都会有所帮助。
谢谢。
I have a few questions about validation in Models and Forms. Could you help me out with these:
Where should validation be done? Should it be in the Model or the Form? Is the right way to go about this is to have validators in the form and constraints in the mode?
What is the difference between writing a 'clean_' method in the form and writing a validator? I've seen that people often put validation checks in the 'clean_' method.
In the request that I'm handling, I have a param in the URL string called 'alive'. This is generally 1 or 0. What would be the correct way of defining this in my form? I need to validate it is a number and can only be 1 or 0. Is this the right way?
alive = models.IntegerField(null=False, max_value=1, min_value=0)
How do I define a default value for this field i.e. if this parameter isn't passed, I default to 0 (False).
I don't have a form on the client side. I'm using the Django form the validate my JS POST request.
In one of model fields I need to store screen resolution in the format 1234x4321. Should I declare this as a CharField
add some regular expression validation in both the Model and the Form? Any examples of regular expression validations would be helpful.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
验证应该在表单上完成,而不是在模型上完成。但是,如果您使用 ModelForms(这通常很有意义),它将从模型本身继承一些验证规则(特定于数据库的规则,例如最大字段长度、数据库字段类型,而且如果它们可以留空)。
字段的默认值应与其构造函数一起传递:
尽管在您的情况下,如果可以获得的值只有零和一,则使用
BooleanField
是有意义的相反(在这种情况下它将默认为 false)。对于分辨率,我将在可能的分辨率和某个任意值之间创建映射。
然后将其传递给模型:
以便用户获得带有相应选项和值的选择字段。
另一方面,如果您需要用户自己插入它,那么使用两个字段(一个用于宽度,另一个用于高度)将比验证用户输入容易得多。
所以你可以为模型定义一个方法:
The validation should be done on the form, not the model. However, if you are using ModelForms, which is usually makes a lot of sense, it will inherit some of the validation rules from the models themselves (those specific to the database, like maximum_field length, database field type, but also if they can be left blank).
The default value of a field should be passed with its constructor:
Although in your case, it appears that if the values that can be obtained are only zero and one, it would be make sense to use a
BooleanField
instead(tand in that case it would default to false).In the case of resolutions I would create a mapping between the possible resolution and some arbitrary value.
and then pass it to the model:
So that the user gets a select field with the corresponding options and values.
On the other hand, if you need the user to insert it him/herself, using two fields (one for width, another for height) would be much easier than validating the user input.
So you can define a method for the model: