@deconstructible
class TestValidator(object):
def __call__(self, foo):
raise ValidationError(message = _("Expected Error"))
class DummyModel(models.Model):
dummyJson = JSONField(validators = [TestValidator])
>>> from Dummy.models import DummyModel
>>> import json
>>> dummyJsonInput = json.loads("""{"foo" : "bar"}""")
>>> dummyModelInstance = DummyModel(dummyJson = dummyJsonInput)
>>> dummyModelInstance.full_clean()
>>>
-> dummyModelInstance.full_clean()
c:\program files\python35\lib\site-packages\django\db\models\base.py(1210)full_clean()
-> self.clean_fields(exclude=exclude)
c:\program files\python35\lib\site-packages\django\db\models\base.py(1252)clean_fields()
-> setattr(self, f.attname, f.clean(raw_value, self))
c:\program files\python35\lib\site-packages\django\db\models\fields\__init__.py(592)clean()
-> self.run_validators(value)
c:\program files\python35\lib\site-packages\django\db\models\fields\__init__.py(544)run_validators()
-> v(value)
> c:\program files\python35\lib\site-packages\django\utils\deconstruct.py(16)__new__()
-> def __new__(cls, *args, **kwargs):
(Pdb) ll
16 -> def __new__(cls, *args, **kwargs):
17 # We capture the arguments to make returning them trivial
18 obj = super(klass, cls).__new__(cls)
19 obj._constructor_args = (args, kwargs)
20 return obj
So I've dug into this a little more, and I've come up with the debugger trace below.
-> dummyModelInstance.full_clean()
c:\program files\python35\lib\site-packages\django\db\models\base.py(1210)full_clean()
-> self.clean_fields(exclude=exclude)
c:\program files\python35\lib\site-packages\django\db\models\base.py(1252)clean_fields()
-> setattr(self, f.attname, f.clean(raw_value, self))
c:\program files\python35\lib\site-packages\django\db\models\fields\__init__.py(592)clean()
-> self.run_validators(value)
c:\program files\python35\lib\site-packages\django\db\models\fields\__init__.py(544)run_validators()
-> v(value)
> c:\program files\python35\lib\site-packages\django\utils\deconstruct.py(16)__new__()
-> def __new__(cls, *args, **kwargs):
(Pdb) ll
16 -> def __new__(cls, *args, **kwargs):
17 # We capture the arguments to make returning them trivial
18 obj = super(klass, cls).__new__(cls)
19 obj._constructor_args = (args, kwargs)
20 return obj
For some reason, when run_validators() calls the custom TestValidator, it seems that a TestValidator object is constructed rather than entering the __call__() method of the class. I can't see a reason for this to happen, so what am I doing wrong here?
Thanks,
-Ryan Causey
class DummyModel(models.Model):
dummyJson = JSONField(validators = [TestValidator])#Using class, wrong!
class DummyModel(models.Model):
dummyJson = JSONField(validators = [TestValidator()])#Using instance of class, right!