I'm writing a custom validator that receives a list of validators, and checks if any of them passes, so you can easily check for things like this integer field should be lower than 10 or higher than 100. This is what I have so far:
class ValidateAnyOf(object):
def __init__(self, *validators):
self.validators = validators
def __call__(self, value):
errors = []
for validator in self.validators:
try:
validator(value)
return
except ValidationError as e:
errors.append(e.message)
raise ValidationError('Combined validation failed:' + ','.join(errors))
class TestModel(models.Model):
score = models.IntegerField(validators=[ValidateAnyOf(validators.MaxValueValidator(10),
validators.MinValueValidator(100))])
The logic is ok. The problem arises when none of the validators passes. I want to show a list of the errors, so I store in a list the messages from the exceptions. The problem is that e.message seems to be a django.utils.functional.__proxy__ object, instead of a string, so I can't join them to show the full error message.
How can I get the error message from each exception?