每個字段可以添加
fromdjango.core.exceptionsimportValidationError
fromdjango.utils.translationimportugettext_lazyas_
defvalidate_even(value):
ifvalue%2!=0:
raiseValidationError(
_('%(value)sisnotanevennumber'),
params={'value':value},
)
fromdjango.dbimportmodels
classMyModel(models.Model):
even_field=models.IntegerField(validators=[validate_even])