Saturday, August 27, 2016

Re: Validators and django.contrib.postgres.fields JSONField

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

On Sunday, August 21, 2016 at 9:32:50 PM UTC-7, Ryan Causey wrote:
Hello,

I've stumbled upon an issue when using JSONField from django.contrib.postgres.fields where I can't get it to run my additional validators when calling full_clean(). To show a simplified example:

validators.py:

@deconstructible
class TestValidator(object):
   
def __call__(self, foo):
       
raise ValidationError(message = _("Expected Error"))


models.py:

class DummyModel(models.Model):
    dummyJson
= JSONField(validators = [TestValidator])


shell output:

>>> from Dummy.models import DummyModel
>>> import json
>>> dummyJsonInput = json.loads("""{"foo" : "bar"}""")
>>> dummyModelInstance = DummyModel(dummyJson = dummyJsonInput)
>>> dummyModelInstance.full_clean()
>>>


Am I doing something wrong? I've read some documentation about needing to define __eq__ for migration purposes, but I tried defining it and it didn't seem to make a difference.

Any help is appreciated.

Thanks,
-Ryan Causey

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e0460414-4ec6-4711-ad24-508c5740e140%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment