Skip to content

Latest commit

 

History

History
18 lines (16 loc) · 533 Bytes

validator表单验证.md

File metadata and controls

18 lines (16 loc) · 533 Bytes

基础

from django.core.exceptions import ValidationError
from django.core import validators

直接使用

validators.validate_email("ramwin@qq.com")

model使用

def validate_even(value):
    if value %2 != 0:
        raise ValidationError(
            _('%(value)s is not an even number'),
            params= {'value': value},
        )
class MyModel(models.Model):
    even_field = models.IntegerField(validators=[validate_even])
a = MyModel(even_field=1)
a.full_clean()