Add support for initial dictionary and list values for JSONField.

Review Request #10351 — Created Dec. 6, 2018 and submitted — Latest diff uploaded

chipx86
Djblets
release-1.0.x
b2e673a...
djblets

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
JSONFields, 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 or list. If
so, it clones it and sets it directly. Otherwise, it proceeds to handle
JSON-encoded strings or None 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.

Loading...