Django - How to specify which field a validation fails on?

后端 未结 6 1386
太阳男子
太阳男子 2021-01-31 01:43

I have this model I\'m showing in the admin page:

class Dog(models.Model):
    bark_volume = models.DecimalField(...
    unladen_speed = models.DecimalField(...
         


        
6条回答
  •  悲&欢浪女
    2021-01-31 02:17

    The simplest way to validate this particular case would be:

    from django.core.validators import MinValueValidator
    from django.utils.translation import ugettext_lazy as _
    
    class Dog(models.Model):
        bark_volume = models.DecimalField(
            ..., validators=[MinValueValidator(5, message=_("Must be louder!"))]
    

    Django's documentation about validators: https://docs.djangoproject.com/en/dev/ref/validators/

提交回复
热议问题