如何在管理员中显示自定义错误

时间:2017-06-06 22:45:18

标签: python django django-models django-admin

我在pre_save中提出了validationError但是每当我尝试创建一个新对象时,我都会得到一个带有错误的黄页。

@receiver(pre_save, sender=Student)
def student_pre_save(sender,instance, **kwargs):

    if instance.classroom.student_set.count() == instance.classroom.QttMax_Stu:
        raise ValidationError("No places left")

1 个答案:

答案 0 :(得分:0)

def clean(self, *args, **kwargs):
    try:
        super(Student, self).save(*args, **kwargs)
    except ValidationError:
        raise ValidationError("No places left")

我把它放在模型中并且效果很好。