SubfieldBase/to_python handles both serialized and original string which breaks custom model fields like JSONField
|Reported by:||bjasper@…||Owned by:||nobody|
|Severity:||Normal||Keywords:||subfieldbase to_python custom field json jsonfield|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
When using a custom model field liked JSONField (a field that lets you store validated JSON in your model), the way to_python() method handles both the serialized and original string makes it difficult (impossible?) to determine which case we're handling––so we never know if a string is still encoded JSON or has already been decoded.
This is a problem, for example, when storing raw numbers.
If I store the raw number 13, I'll get back the number 13.
But if I store the string "13", I'll still get back the number 13. This is because to_python() receives the "13", see's it's a string, thinks it's JSON, and decodes it.
It doesn't know if "13" should be returned as a string or returned as a number since to_python() doesn't know the context in which it's being called.
The only way we've found to get around this on our end is to prepend JSON encoded strings with the text "json:". This way we'd know for sure if a string was still encoded JSON. This is a big hack, so we'd like to avoid it as much as possible.
I have some tests but they are tied to the existing test suite––hope that's OK:
git clone -b broken_to_python https://github.com/bradjasper/django-jsonfield.git
python setup.py test
Original discussion started here: https://github.com/bradjasper/django-jsonfield/issues/33