【问题标题】:How to show custom error in the admin如何在管理员中显示自定义错误
【发布时间】:2017-06-06 22:45:18
【问题描述】:

我在 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")

【问题讨论】:

    标签: python django django-models django-admin


    【解决方案1】:
    def clean(self, *args, **kwargs):
        try:
            super(Student, self).save(*args, **kwargs)
        except ValidationError:
            raise ValidationError("No places left")
    

    我已将其放入模型中,效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-03
      • 2011-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多