Add support for initial dictionary and list values for JSONField.
Review Request #10351 — Created Dec. 6, 2018 and submitted — Latest diff uploaded
JSONField
can now accept an initial dictionary or list value (rather
than just a JSON-encoded string) when creating an instance of a model.
Previously, callers had to create the model and then set any
JSONField
s, but this meant that JSON data couldn't be set in
Model.objects.get_or_create()
calls unless that data was first
JSON-encoded (which could be unnecessarily expensive).Now, the field first checks if it's dealing with a
dict
orlist
. If
so, it clones it and sets it directly. Otherwise, it proceeds to handle
JSON-encoded strings orNone
values as it did before, and then falls
back to raising an error if the type wasn't expected.
Unit tests passed.
Used this in some code I'm writing that passes JSON-encodable data
during model instantiation.