summaryrefslogtreecommitdiff
path: root/django/db/models/query_utils.py
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2019-07-23 05:04:06 -0700
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-07-25 07:24:52 +0200
commit93ffa81bc59ff13c1a5c07f5bd22d003756e1da5 (patch)
treec1c4ceca7b900c36f36458df44631676340c8564 /django/db/models/query_utils.py
parentfc75694257b5bceab82713f84fe5a1b23d641c3f (diff)
Refs #30657 -- Made DeferredAttribute.__init__() to take a field instance instead of a field name.
Diffstat (limited to 'django/db/models/query_utils.py')
-rw-r--r--django/db/models/query_utils.py24
1 files changed, 12 insertions, 12 deletions
diff --git a/django/db/models/query_utils.py b/django/db/models/query_utils.py
index 90289d0da2..7a667814f4 100644
--- a/django/db/models/query_utils.py
+++ b/django/db/models/query_utils.py
@@ -116,8 +116,8 @@ class DeferredAttribute:
A wrapper for a deferred-loading field. When the value is read from this
object the first time, the query is executed.
"""
- def __init__(self, field_name):
- self.field_name = field_name
+ def __init__(self, field):
+ self.field = field
def __get__(self, instance, cls=None):
"""
@@ -127,26 +127,26 @@ class DeferredAttribute:
if instance is None:
return self
data = instance.__dict__
- if data.get(self.field_name, self) is self:
+ field_name = self.field.attname
+ if data.get(field_name, self) is self:
# Let's see if the field is part of the parent chain. If so we
# might be able to reuse the already loaded value. Refs #18343.
- val = self._check_parent_chain(instance, self.field_name)
+ val = self._check_parent_chain(instance)
if val is None:
- instance.refresh_from_db(fields=[self.field_name])
- val = getattr(instance, self.field_name)
- data[self.field_name] = val
- return data[self.field_name]
+ instance.refresh_from_db(fields=[field_name])
+ val = getattr(instance, field_name)
+ data[field_name] = val
+ return data[field_name]
- def _check_parent_chain(self, instance, name):
+ def _check_parent_chain(self, instance):
"""
Check if the field value can be fetched from a parent field already
loaded in the instance. This can be done if the to-be fetched
field is a primary key field.
"""
opts = instance._meta
- f = opts.get_field(name)
- link_field = opts.get_ancestor_link(f.model)
- if f.primary_key and f != link_field:
+ link_field = opts.get_ancestor_link(self.field.model)
+ if self.field.primary_key and self.field != link_field:
return getattr(instance, link_field.attname)
return None