3👍
✅
Field validators are stored in field.validators
, it’s a list, so you basically need to append your validators there.
To access the field instance, you’ll have to play a little bit with the _meta
attribute of your object (note that you’re not supposed to play with this attribute, so when you update django, you’ll have to check that it has not changed). Here’s how you could do it:
def get_fields_dict(self):
return dict((field.name, field) for field in self._meta.fields)
def __init__(self, *args, **kwargs):
super(GlobalTeplate, self).__init__(*args, **kwargs)
text_field = self.get_fields_dict()['text']
text_field.validators.append(validate_custom)
0👍
You can add a clean_fields()
method to the child model with extra validators.
Source:stackexchange.com