For example, here’s a validator that only allows even numbers:

from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

def validate_even(value):
    if value % 2 != 0:
        raise ValidationError(
            _('%(value)s is not an even number'),
            params={'value': value},
        )

You can add this to a model field via the field’s validators argument:

from django.db import models

class MyModel(models.Model):
    even_field = models.IntegerField(validators=[validate_even])


https://docs.djangoproject.com/en/3.1/ref/validators/

相关文章:

  • 2022-01-10
  • 2021-12-06
  • 2022-01-12
  • 2021-07-20
  • 2022-01-10
  • 2021-11-10
  • 2021-11-11
  • 2021-11-08
猜你喜欢
  • 2022-01-13
  • 2021-11-12
  • 2021-05-13
  • 2021-04-25
  • 2021-06-17
  • 2022-12-23
相关资源
相似解决方案