Python 如何在管理员界面中显示自定义错误
我在pre_save中引发了一个validationError,但是每当我尝试创建一个新对象时,我都会看到一个带有错误的黄页,而不是像其他对象一样出现红色错误Python 如何在管理员界面中显示自定义错误,python,django,django-models,django-admin,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:
@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")
我把这个放在模型里,效果很好
def clean(self, *args, **kwargs):
try:
super(Student, self).save(*args, **kwargs)
except ValidationError:
raise ValidationError("No places left")