diff options
| author | Anssi Kääriäinen <akaariai@gmail.com> | 2012-06-26 18:08:42 +0300 |
|---|---|---|
| committer | Anssi Kääriäinen <akaariai@gmail.com> | 2012-06-26 18:08:42 +0300 |
| commit | b6c356b7bb97f3d6d4831b31e67868313bbbc090 (patch) | |
| tree | 1cd4c587080703c5b86b13349858509ce145ca4c /django/db/models/sql | |
| parent | 531878302735e6a2b36d82b584947bbf8eae8111 (diff) | |
Fixed #17485 -- Made defer work with select_related
This commit tackles a couple of issues. First, in certain cases there
were some mixups if field.attname or field.name should be deferred.
Field.attname is now always used.
Another issue tackled is a case where field is both deferred by
.only(), and selected by select_related. This case is now an error.
A lot of thanks to koniiiik (Michal Petrucha) for the patch, and
to Andrei Antoukh for review.
Diffstat (limited to 'django/db/models/sql')
| -rw-r--r-- | django/db/models/sql/compiler.py | 7 | ||||
| -rw-r--r-- | django/db/models/sql/query.py | 12 |
2 files changed, 14 insertions, 5 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 5801b2f428..d44cdfe4a4 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -596,6 +596,7 @@ class SQLCompiler(object): if avoid_set is None: avoid_set = set() orig_dupe_set = dupe_set + only_load = self.query.get_loaded_field_names() # Setup for the case when only particular related fields should be # included in the related selection. @@ -607,7 +608,8 @@ class SQLCompiler(object): restricted = False for f, model in opts.get_fields_with_model(): - if not select_related_descend(f, restricted, requested): + if not select_related_descend(f, restricted, requested, + only_load.get(model or self.query.model)): continue # The "avoid" set is aliases we want to avoid just for this # particular branch of the recursion. They aren't permanently @@ -680,7 +682,8 @@ class SQLCompiler(object): if o.field.unique ] for f, model in related_fields: - if not select_related_descend(f, restricted, requested, reverse=True): + if not select_related_descend(f, restricted, requested, + only_load.get(model), reverse=True): continue # The "avoid" set is aliases we want to avoid just for this # particular branch of the recursion. They aren't permanently diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 7f331bfe7f..8fbba3dbc9 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -1845,9 +1845,15 @@ class Query(object): If no fields are marked for deferral, returns an empty dictionary. """ - collection = {} - self.deferred_to_data(collection, self.get_loaded_field_names_cb) - return collection + # We cache this because we call this function multiple times + # (compiler.fill_related_selections, query.iterator) + try: + return self._loaded_field_names_cache + except AttributeError: + collection = {} + self.deferred_to_data(collection, self.get_loaded_field_names_cb) + self._loaded_field_names_cache = collection + return collection def get_loaded_field_names_cb(self, target, model, fields): """ |
