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?