MyModel fields ValidationError clean_fields

class MyModel(models.Model):
foo = models.BooleanField()

# Wrong!  Don't do this!
def clean_fields(self, exclude=None): # 指定某些字段排除验证
    # Bah!  Forgetting to pass exclude defaults it to None!
    super(MyModel, self).clean_fields()
    if not self.foo:
        raise ValidationError('Ah fooey.')

# Correct!  Do it this way!
def clean_fields(self, exclude=None):
    # That's better!  Let's take a coffee break.  I'm tired.
    super(MyModel, self).clean_fields(exclude)
    if not self.foo:
        raise ValidationError('Ah fooey.')
posted @ 2022-10-28 16:04  悠然采  阅读(12)  评论(0编辑  收藏  举报