有没有办法以标准方式显示验证错误,因为其他错误?
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')
答案 0 :(得分:2)
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')