TLDR;
How do I provide custom, model level, field validation which is encapsulated inside the field class?
The Rest
I am playing with the two JSONField implementations, (first, second). I am using a combination of Django and Django REST framework for my application. I don't do anything on the form level, only a web API exposing the model.
Ideally I would like to write this validation in one place and for it to run on the serializer level + model level (like when I pass a validators=[x]
). I can't use the usual validators=[x]
because I need to allow blank=True
but also validate the blank values type :|.
I have the use case that I want to validate the contents (keys, types of values) of my JSONField
. Using validictory, I can do this like so:
- force a call to
self.full_clean()
in mysave()
- override
clean()
on my model and do the validation there
However, what I really want to do is: add this validation to a subclass of the JSONField
. I want to leave as much of the parent Field
class to do it's thing. So far, I have:
from django.db import models
from jsonfield import JSONField
class myValidatorField(JSONField):
def validate(self, *args, **kwargs):
# do some validation here
super(myValidatorField, self).validate(*args, **kwargs)
class MyModel(models.Model):
jsonstuff = myValidatorField(default=[])
def save(self, *args, **kwargs):
self.full_clean()
super(MyModel, self).save(*args, **kwargs)
However, I can't get this to work. This validate()
method doesn't run for the second implementation, and for the first, it runs 4 times.
Confused.