summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2015-09-19 22:50:57 +0200
committerAymeric Augustin <aymeric.augustin@m4x.org>2015-09-21 22:06:04 +0200
commit2409a4241a07bcdb1be4f3c99bf1aacc87189ebf (patch)
treef3271a89318a61c8a0205818ca2598927ee695e1
parent497d9152997252f0afe172e3421cbfe4fbe55780 (diff)
Added comments in related instances descriptors.
-rw-r--r--django/db/models/fields/related_descriptors.py29
1 files changed, 23 insertions, 6 deletions
diff --git a/django/db/models/fields/related_descriptors.py b/django/db/models/fields/related_descriptors.py
index 91f17ef53e..926704ad7d 100644
--- a/django/db/models/fields/related_descriptors.py
+++ b/django/db/models/fields/related_descriptors.py
@@ -148,6 +148,9 @@ class ReverseSingleRelatedObjectDescriptor(object):
if instance is None:
return self
+ # The related instance is loaded from the database and then cached in
+ # the attribute defined in self.cache_name. It can also be pre-cached
+ # by the reverse accessor (SingleRelatedObjectDescriptor).
try:
rel_obj = getattr(instance, self.cache_name)
except AttributeError:
@@ -159,6 +162,9 @@ class ReverseSingleRelatedObjectDescriptor(object):
qs = qs.filter(**self.field.get_reverse_related_filter(instance))
# Assuming the database enforces foreign keys, this won't fail.
rel_obj = qs.get()
+ # If this is a one-to-one relation, set the reverse accessor
+ # cache on the related object to the current instance to avoid
+ # an extra SQL query if it's accessed later on.
if not self.field.remote_field.multiple:
setattr(rel_obj, self.field.remote_field.get_cache_name(), instance)
setattr(instance, self.cache_name, rel_obj)
@@ -232,10 +238,13 @@ class ReverseSingleRelatedObjectDescriptor(object):
for lh_field, rh_field in self.field.related_fields:
setattr(instance, lh_field.attname, getattr(value, rh_field.attname))
- # Since we already know what the related object is, seed the related
- # object caches now, too. This avoids another db hit if you get the
- # object you just set.
+ # Set the related instance cache used by __get__ to avoid a SQL query
+ # when accessing the attribute we just set.
setattr(instance, self.cache_name, value)
+
+ # If this is a one-to-one relation, set the reverse accessor cache on
+ # the related object to the current instance to avoid an extra SQL
+ # query if it's accessed later on.
if value is not None and not self.field.remote_field.multiple:
setattr(value, self.field.remote_field.get_cache_name(), instance)
@@ -312,6 +321,9 @@ class SingleRelatedObjectDescriptor(object):
if instance is None:
return self
+ # The related instance is loaded from the database and then cached in
+ # the attribute defined in self.cache_name. It can also be pre-cached
+ # by the forward accessor (ReverseSingleRelatedObjectDescriptor).
try:
rel_obj = getattr(instance, self.cache_name)
except AttributeError:
@@ -325,6 +337,9 @@ class SingleRelatedObjectDescriptor(object):
except self.related.related_model.DoesNotExist:
rel_obj = None
else:
+ # Set the forward accessor cache on the related object to
+ # the current instance to avoid an extra SQL query if it's
+ # accessed later on.
setattr(rel_obj, self.related.field.get_cache_name(), instance)
setattr(instance, self.cache_name, rel_obj)
@@ -386,10 +401,12 @@ class SingleRelatedObjectDescriptor(object):
for index, field in enumerate(self.related.field.local_related_fields):
setattr(value, field.attname, related_pk[index])
- # Since we already know what the related object is, seed the related
- # object caches now, too. This avoids another db hit if you get the
- # object you just set.
+ # Set the related instance cache used by __get__ to avoid a SQL query
+ # when accessing the attribute we just set.
setattr(instance, self.cache_name, value)
+
+ # Set the forward accessor cache on the related object to the current
+ # instance to avoid an extra SQL query if it's accessed later on.
setattr(value, self.related.field.get_cache_name(), instance)