Issue
I have been playing around with my test project
I have this clean method in my model
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',]})
I don't really know how to raise both errors and show it in the admin page because even if the two if
is True
, only the first if
error is shown(obviously) how do I show both errors?
Solution
You could build a dict
of errors and raise a ValidationError when you are done (if necessary):
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)
Answered By - user2390182
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.