2

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?

2 Answers 2

9

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Link to the docs that recommends this, "If you detect errors in multiple fields during Model.clean(), you can also pass a dictionary mapping field names to errors" (Note that the dict values are validation errors)
Link to the above quote for the docs of newer version of Django (1.9 docs no longer available): docs.djangoproject.com/en/4.0/ref/models/instances/…
The problem with this solution is that if you have to raise multiple errors on one field (say because a related field is erroneous), you will override your first error message.
0

I would expand the accepted answer by setting a classmethod like this:

@classmethod
def add_error(cls, errordict, error):
    for key, value in error.items():
        if key in errordict:
            if isinstance(errordict[key], str) or isinstance(errordict[key], ValidationError):
                errordict.update({key: [errordict[key], value]})
            elif isinstance(errordict[key], list):
                errordict[key].append(value)
        else:
            errordict.update(error)
    return errordict

That way, if a key already exists in your error dict, it will be casted in a list. That way, all the errors will be raised, even if there are multiple errors per field.

adderror(error_dict, {'field', 'error message'})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.