我一直在玩我的测试项目
我的模型中有这个干净的方法
class SomeModel(models.Model):
f1 = models.IntegerField()
f2 = models.IntegerField()
def clean(self):
if self.f1 > self.f2:
raise ValidationError({'f1': ['Should be greater than f1',]})
if self.f2 == 100:
raise ValidationError({'f2': ['That's too much',]})
我真的不知道如何引发这两个错误并在管理页面中显示错误,因为即使两个if
为True
,也只有第一个if
错误是显示(显然)我如何显示两个错误?
答案 0 :(得分:1)
您可以构建dict
个错误并在完成后引发ValidationError(如有必要):
class SomeModel(models.Model):
f1 = models.IntegerField()
f2 = models.IntegerField()
def clean(self):
error_dict = {}
if self.f1 > self.f2:
error_dict['f1'] = ValidationError("Should be greater than f1") # this should probably belong to f2 as well
if self.f2 == 100:
error_dict['f2'] = ValidationError("That's too much")
if error_dict:
raise ValidationError(error_dict)