Support deferred attributes in JSONField and improve Django compatibility.
Review Request #10469 — Created March 27, 2019 and submitted
JSONField was prone to unwanted queries when working with models
that defferred the JSONField. This wasn't so much a major problem on
Django 1.6, but manifested on newer versions of Django. The core issue
was that, during the model initialization phase,JSONField
would try
to load data from the model in order to normalize it, and if the field
was deferred, this act would trigger a query. To fix this, this change
now checks whether the attribute is deferred before performing any
lookups.Other changes to the parent
TextField
in newer versions of Django
broke other aspects ofJSONField
.TextField.get_prep_value()
was
added, which forced the value to be a string, and in our case this
resulted in some unwanted serialization. We now override this, instead
ofget_db_prep_value()
(which will, in the default implementation,
calls our new method and do the right thing).On the other end of that process is the
to_python()
method, which we
weren't overriding. We now provide this, and have it return a
deserialized representation of the data.
Djblets and Review Board unit tests pass.