1from django.core.exceptions import ValidationError
2from django.utils.translation import gettext_lazy as _
3
4def validate_even(value):
5 if value % 2 != 0:
6 raise ValidationError(
7 _('%(value)s is not an even number'),
8 params={'value': value},
9 )
10
1from django.db import models
2
3class MyModel(models.Model):
4 even_field = models.IntegerField(validators=[validate_even])
5