gpt4 book ai didi

python - 如何将验证错误显示为 django admin 默认错误

转载 作者:行者123 更新时间:2023-11-30 22:36:41 32 4
gpt4 key购买 nike

有什么方法可以像其他错误一样以标准方式显示验证错误吗?

class Student(models.Model):
...

def save(self, *args, **kwargs):
try:
super(Student, self).save(*args, **kwargs)
except ValidationError:
raise forms.ValidationError("There aren't places anymore")

最佳答案

from django.core.exceptions import ValidationError

class MyModel(models.Model):

def is_available(self):
#do check here
return result

def clean(self):
if not self.is_available():
raise ValidationError('Item already booked for those dates')

关于python - 如何将验证错误显示为 django admin 默认错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44081786/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com