我在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")
答案 0 :(得分:0)
def clean(self, *args, **kwargs):
try:
super(Student, self).save(*args, **kwargs)
except ValidationError:
raise ValidationError("No places left")
我把它放在模型中并且效果很好。