简体   繁体   English

在Django中clean是什么意思?

[英]What does clean mean in Django?

This is an obvious question for everyone, but I don't understand what the term "clean" means. 对于每个人来说,这都是一个显而易见的问题,但我不明白“干净”一词的含义。 I can use clean_data, and use form validation on my forms. 我可以使用clean_data,并在表单上使用表单验证。 However, I still don't understand what this means. 但是,我仍然不明白这意味着什么。

In order to use validation, do I always need to use the keyword "clean"? 为了使用验证,我是否总是需要使用关键字“ clean”?

As stated in documentation 文档中所述

The clean() method on a Field subclass is responsible for running to_python(), validate(), and run_validators() in the correct order and propagating their errors. Field子类的clean()方法负责以正确的顺序运行to_python(),validate()和run_validators()并传播其错误。 If, at any time, any of the methods raise ValidationError, the validation stops and that error is raised. 如果任何时候任何方法引发ValidationError,则验证将停止并引发该错误。 This method returns the clean data, which is then inserted into the cleaned_data dictionary of the form. 此方法返回清除数据,然后将其插入表单的cleaned_data字典中。

Clean is preventing dirty data in DB 清除可防止数据库中的脏数据

As far as i have understood ..all the implicit validations are performed using clean.. which is performed when we check for the form validity using is_valid: 据我了解..所有隐式验证均使用clean ..执行,当我们使用is_valid检查表单有效性时执行:

but we can add our own validations to it by overriding the function clean(): what we have to do is we call the super().clean() such that all the implicit validations are done by the clean() defined by django and then add our own validations to it if necessary... 但是我们可以通过覆盖函数clean()来添加自己的验证:我们要做的是调用super()。clean(),以便所有隐式验证都由django和然后在必要时添加我们自己的验证...

when you call the super().clean() it returns the dictionary containing the cleaned_data.. you can store it in a variable ... or else you can access the dictionary using self.cleaned_data 当您调用super()。clean()时,它将返回包含cleaned_data的字典。您可以将其存储在变量中……否则,您可以使用self.cleaned_data访问字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM