当前位置 主页 > 网站技术 > 代码类 >

    Django框架model模型对象验证实现方法分析

    栏目:代码类 时间:2019-10-09 21:02

    本文实例讲述了Django框架model模型对象验证实现方法。分享给大家供大家参考,具体如下:

    模型对象的验证

    验证一个模型涉及三个步骤:

    验证模型的字段 —— Model.clean_fields() 验证模型的完整性 —— Model.clean() 验证模型的唯一性 —— Model.validate_unique()

    当调用模型的full_clean() 方法时,这三个方法都将执行。当使用ModelForm时,is_valid() 将为表单中的所有字段执行这些验证。如果你计划自己处理验证出现的错误,或者你已经将需要验证的字段从ModelForm 中去除掉,你只需调用模型的full_clean() 方法。

    Model.full_clean(exclude=None, validate_unique=True)

    该方法按顺序调用Model.clean_fields()、Model.clean() 和Model.validate_unique()(如果validate_unique 为True),并引发一个ValidationError,该异常的message_dict 属性包含三个步骤的所有错误。可选的exclude 参数用来提供一个可以从验证和清除中排除的字段名称的列表。ModelForm 使用这个参数来排除表单中没有出现的字段,使它们不需要验证,因为用户无法修正这些字段的错误。注意,当你调用模型的save() 方法时,full_clean() 不会 自动调用。如果你想一步就可以为你手工创建的模型运行验证,你需要手工调用它。例如:

    from django.core.exceptions import ValidationError
    try:
      article.full_clean()
    except ValidationError as e:
      # Do something based on the errors contained in e.message_dict.
      # Display them to a user, or handle them programmatically.
      pass
    
    

    full_clean() 第一步执行的是验证每个字段。

    Model.clean_fields(exclude=None)

    这个方法将验证模型的所有字段。可选的exclude 参数让你提供一个字段名称列表来从验证中排除。如果有字段验证失败,它将引发一个ValidationError。

    full_clean() 第二步执行的是调用Model.clean()。如要实现模型自定义的验证,应该覆盖这个方法。

    Model.clean()

    应该用这个方法来提供自定义的模型验证,以及修改模型的属性。例如,你可以使用它来给一个字段自动提供值,或者用于多个字段需要一起验证的情形:

    import datetime
    from django.core.exceptions import ValidationError
    from django.db import models
    class Article(models.Model):
      ...
      def clean(self):
        # Don't allow draft entries to have a pub_date.
        if self.status == 'draft' and self.pub_date is not None:
          raise ValidationError('Draft entries may not have a publication date.')
        # Set the pub_date for published items if it hasn't been set already.
        if self.status == 'published' and self.pub_date is None:
          self.pub_date = datetime.date.today()
    
    

    Model.full_clean() 类似,调用模型的save() 方法时不会引起clean() 方法的调用。

    在上面的示例中,Model.clean() 引发的ValidationError 异常通过一个字符串实例化,所以它将被保存在一个特殊的错误字典键NON_FIELD_ERRORS中。这个键用于整个模型出现的错误而不是一个特定字段出现的错误:

    from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
    try:
      article.full_clean()
    except ValidationError as e:
      non_field_errors = e.message_dict[NON_FIELD_ERRORS]