Support deferred attributes in JSONField and improve Django compatibility.

Review Request #10469 — Created March 27, 2019 and submitted

Information

Djblets
release-2.0.x

Reviewers

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 of JSONField. 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
of get_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.

Summary ID
Support deferred attributes in JSONField and improve Django compatibility.
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 of `JSONField`. `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 of `get_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.
676c7f21e9bd03ec3aa66c55654837a0d704714d
Description From Last Updated

F401 'django.db.models.query_utils.DeferredAttribute' imported but unused

reviewbotreviewbot

F841 local variable 'site' is assigned to but never used

reviewbotreviewbot
Checks run (1 failed, 1 succeeded)
flake8 failed.
JSHint passed.

flake8

chipx86
david
  1. Ship It!
  2. 
      
chipx86
Review request changed

Status: Closed (submitted)

Change Summary:

Pushed to release-2.0.x (efd7ebe)
Loading...