Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

queryset-refactor: Fixed a possibility of shooting oneself in the foo…

…t and

creating infinite recursion with select_related(). Refs #3045, #3288.


git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@6521 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 8c10e0d00e944b7c699530f0aeb98de64dc8316a 1 parent 3429fc0
@malcolmt malcolmt authored
View
3  django/db/models/query.py
@@ -294,7 +294,8 @@ def select_related(self, true_or_false=True, depth=0):
"""Returns a new QuerySet instance that will select related objects."""
obj = self._clone()
obj.query.select_related = true_or_false
- obj.query.max_depth = depth
+ if depth:
+ obj.query.max_depth = depth
return obj
def order_by(self, *field_names):
View
5 django/db/models/sql/query.py
@@ -87,7 +87,10 @@ def __init__(self, model, connection):
self.low_mark, self.high_mark = 0, None # Used for offset/limit
self.distinct = False
self.select_related = False
- self.max_depth = 0
+
+ # Arbitrary maximum limit for select_related to prevent infinite
+ # recursion. Can be changed by the depth parameter to select_related().
+ self.max_depth = 5
# These are for extensions. The contents are more or less appended
# verbatim to the appropriate clause.
View
19 tests/regressiontests/queries/models.py
@@ -80,6 +80,14 @@ class Meta:
def __unicode__(self):
return self.title
+# Some funky cross-linked models for testing a couple of infinite recursion
+# cases.
+class X(models.Model):
+ y = models.ForeignKey('Y')
+
+class Y(models.Model):
+ x1 = models.ForeignKey(X, related_name='y1')
+
__test__ = {'API_TESTS':"""
>>> t1 = Tag(name='t1')
>>> t1.save()
@@ -353,9 +361,18 @@ def __unicode__(self):
>>> ExtraInfo.objects.values('note')
[{'note': 1}, {'note': 2}]
-# Bug 5261
+Bug #5261
>>> Note.objects.exclude(Q())
[<Note: n1>, <Note: n2>, <Note: n3>]
+Bug #3045, #3288
+Once upon a time, select_related() with circular relations would loop
+infinitely if you forgot to specify "depth". Now we set an arbitrary default
+upper bound.
+>>> X.objects.all()
+[]
+>>> X.objects.select_related()
+[]
+
"""}
Please sign in to comment.
Something went wrong with that request. Please try again.