How can I set required to be false for a django rest framework JSON serializer field? It seems to be enforcing validation regardless of the required flag:
serializer field
results = serializers.JSONField(required=False, label='Result')
model field
results = models.TextField(blank=True, default="")
But when I submit the form with a blank input, I get:
"results": [
"Value must be valid JSON."
],
I've also tried changing the model default to {} in both the model field and the serializer field, but have the same response.
UPDATE
Thanks to @Linovia for pointing out that "The required flag does mean that the serializer will not complain if that field isn't present"
After some digging, it looks like DRF is setting a default value of null on the input, which then is caught as invalid... How can I override this, as the serializer "default" attribute doesn't seem to have any effect.
"results": null,
TextField
,blank=True
is enough. – Ivan Semochkinblank=True
do it for you. It is actually strange logic, why you needJSONField
on top ofTextField
? you can always saveJSON
string in a plain text. If you use postgres and want binary format then useJSONField
in your model. But your code should work, can you add model and serializer in question. – Ivan Semochkin