Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed the way symmetrical many-to-many relations are recorded in the …

…Options class.

These types of relations don't have reverse accessor names, so that name can be
used by a normal field on the model. Fixed #7107.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@7764 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 0e692fda9ca57909216e68a998f2752885c23725 1 parent 279fc85
Malcolm Tredinnick authored June 26, 2008
11  django/db/models/options.py
@@ -274,14 +274,17 @@ def init_name_map(self):
274 274
         """
275 275
         Initialises the field name -> field object mapping.
276 276
         """
277  
-        cache = dict([(f.name, (f, m, True, False)) for f, m in
278  
-                self.get_fields_with_model()])
279  
-        for f, model in self.get_m2m_with_model():
280  
-            cache[f.name] = (f, model, True, True)
  277
+        cache = {}
  278
+        # We intentionally handle related m2m objects first so that symmetrical
  279
+        # m2m accessor names can be overridden, if necessary.
281 280
         for f, model in self.get_all_related_m2m_objects_with_model():
282 281
             cache[f.field.related_query_name()] = (f, model, False, True)
283 282
         for f, model in self.get_all_related_objects_with_model():
284 283
             cache[f.field.related_query_name()] = (f, model, False, False)
  284
+        for f, model in self.get_m2m_with_model():
  285
+            cache[f.name] = (f, model, True, True)
  286
+        for f, model in self.get_fields_with_model():
  287
+            cache[f.name] = (f, model, True, False)
285 288
         if self.order_with_respect_to:
286 289
             cache['_order'] = OrderWrt(), None, True, False
287 290
         if app_cache_ready():
13  tests/regressiontests/queries/models.py
@@ -90,6 +90,15 @@ class Number(models.Model):
90 90
     def __unicode__(self):
91 91
         return unicode(self.num)
92 92
 
  93
+# Symmetrical m2m field with a normal field using the reverse accesor name
  94
+# ("valid").
  95
+class Valid(models.Model):
  96
+    valid = models.CharField(max_length=10)
  97
+    parent = models.ManyToManyField('self')
  98
+
  99
+    class Meta:
  100
+        ordering = ['valid']
  101
+
93 102
 # Some funky cross-linked models for testing a couple of infinite recursion
94 103
 # cases.
95 104
 class X(models.Model):
@@ -768,5 +777,9 @@ class Child(models.Model):
768 777
 >>> len(Tag.objects.order_by('parent__name'))
769 778
 5
770 779
 
  780
+Bug #7107 -- this shouldn't create an infinite loop.
  781
+>>> Valid.objects.all()
  782
+[]
  783
+
771 784
 """}
772 785
 

0 notes on commit 0e692fd

Please sign in to comment.
Something went wrong with that request. Please try again.