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

后端 未结 6 1366
太阳男子
太阳男子 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:22

    OK, I figured it out from this answer.

    You have to do something like this:

    class Dog(models.Model):
        bark_volume = models.DecimalField(...
        unladen_speed = models.DecimalField(...
    
        def clean_fields(self):
            if self.bark_volume < 5:
                raise ValidationError({'bark_volume': ["Must be louder!",]})
    

提交回复
热议问题