0

In my Django model I am using clean() method to validate two sets of fields' values. I am housing both the conditions in the same clean() method. However I find that the first condition is checked by the system and the second one is ignored.

Here is my model and the fields:

class Rates(models.Model):
    master_doc = models.ForeignKey(Origin, ...
    exit_rate = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True, default=0.00)
    from_date = models.DateField(null=True, verbose_name='From date')
    to_date = models.DateField(null=True, verbose_name='To date')

    def clean(self):
        if self.exit_rate <= 0:
            raise ValidationError({'exit_rate': _('The exit rate must be more than 0.')})
        if self.from_date is not None:
            if (self.to_date == self.from_date):
                raise ValidationError({'to_date': _('From Date and end date may not be the same.')})

In this instant case, a validation error is raised only for the first i.e. field exit_rate. If I reverse the order of the check, a validation error is raised for the date fields alone, and not the rate field.

I tried this solution and used error_dict but getting error 'ValidationError' object has no attribute 'error_list'

How do I ensure that validation error is raised in case either of the conditions is not met?

1
  • 1
    Please don't vandalize your own posts. When you post here, you give SO the right to distribute the content under CC-by SA 4.0. Any vandalism will be reverted. Commented Mar 27, 2020 at 17:20

1 Answer 1

6

You will need to test all the conditions first, and then raise one exception containing the multiple messages. Something like

def clean(self):
    errors={}
    if self.exit_rate <= 0:
        errors['exit_rate']= _('The exit rate must be more than 0.')
    if self.from_date is not None:
        if (self.to_date == self.from_date):
            errors['to_date'] = _('From Date and end date may not be the same.')
    if errors:
        raise ValidationError(errors)

I can't immediately find a reference to the exact form of raise ValidationError( things) to pass multiple errors. The above is a guess based on the obvious extension of the raise statement in the original question.

Sign up to request clarification or add additional context in comments.

2 Comments

Your answer is absolute on mark. I had tried the same based on the linked question. Where I was going wrong was to have this error_dict['to_date'] = ValidationError({'to_date': _('From Date and end date may not be same.')}) instead of `error_dict['to_date'] = _('From Date and end date may not be same.'). If you check the linked post, you may be able to see the reason. However, many thanks for help!!
On a second thought, the validation is failing on Update. And funnily enough, in create it worked perfectly. It's the same model, same view and everything. Any particular reason why I may expect this?

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.