Author: mtredinnick
Date: 2007-10-15 00:51:19 -0500 (Mon, 15 Oct 2007)
New Revision: 6521
Modified:
django/branches/queryset-refactor/django/db/models/query.py
django/branches/queryset-refactor/django/db/models/sql/query.py
django/branches/queryset-refactor/tests/regressiontests/queries/models.py
Log:
queryset-refactor: Fixed a possibility of shooting oneself in the foot and
creating infinite recursion with select_related(). Refs #3045, #3288.
Modified: django/branches/queryset-refactor/django/db/models/query.py
===================================================================
--- django/branches/queryset-refactor/django/db/models/query.py 2007-10-15
03:58:20 UTC (rev 6520)
+++ django/branches/queryset-refactor/django/db/models/query.py 2007-10-15
05:51:19 UTC (rev 6521)
@@ -294,7 +294,8 @@
"""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):
Modified: django/branches/queryset-refactor/django/db/models/sql/query.py
===================================================================
--- django/branches/queryset-refactor/django/db/models/sql/query.py
2007-10-15 03:58:20 UTC (rev 6520)
+++ django/branches/queryset-refactor/django/db/models/sql/query.py
2007-10-15 05:51:19 UTC (rev 6521)
@@ -87,8 +87,11 @@
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.
self.extra_select = {} # Maps col_alias -> col_sql.
Modified:
django/branches/queryset-refactor/tests/regressiontests/queries/models.py
===================================================================
--- django/branches/queryset-refactor/tests/regressiontests/queries/models.py
2007-10-15 03:58:20 UTC (rev 6520)
+++ django/branches/queryset-refactor/tests/regressiontests/queries/models.py
2007-10-15 05:51:19 UTC (rev 6521)
@@ -80,6 +80,14 @@
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 @@
>>> 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()
+[]
+
"""}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---