[Django]-Django CreateView and validation

3👍

TL;DR: when specifying this kind of validators in model field definitions, you should pass instances rather than classes (validators.CIDValidator() instead of validators.CIDValidator).

Longer explanation

Django validators need to be callables. Trying to call the class you are passing now will go through python’s creation sequence for an instance, calling __new__ and __init__, and it would return an instance of that class – but it won’t do anything in terms of validating the field value.

The Django validators you are subclassing also have a __call__ method, that is run when you try to call an instance of that class, and it takes care of validating and raising ValidationErrors

Leave a comment