Skip to content

Commit

Permalink
Fixed the way symmetrical many-to-many relations are recorded in the …
Browse files Browse the repository at this point in the history
…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
malcolmt committed Jun 26, 2008
1 parent 279fc85 commit 0e692fd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
11 changes: 7 additions & 4 deletions django/db/models/options.py
Expand Up @@ -274,14 +274,17 @@ def init_name_map(self):
"""
Initialises the field name -> field object mapping.
"""
cache = dict([(f.name, (f, m, True, False)) for f, m in
self.get_fields_with_model()])
for f, model in self.get_m2m_with_model():
cache[f.name] = (f, model, True, True)
cache = {}
# We intentionally handle related m2m objects first so that symmetrical
# m2m accessor names can be overridden, if necessary.
for f, model in self.get_all_related_m2m_objects_with_model():
cache[f.field.related_query_name()] = (f, model, False, True)
for f, model in self.get_all_related_objects_with_model():
cache[f.field.related_query_name()] = (f, model, False, False)
for f, model in self.get_m2m_with_model():
cache[f.name] = (f, model, True, True)
for f, model in self.get_fields_with_model():
cache[f.name] = (f, model, True, False)
if self.order_with_respect_to:
cache['_order'] = OrderWrt(), None, True, False
if app_cache_ready():
Expand Down
13 changes: 13 additions & 0 deletions tests/regressiontests/queries/models.py
Expand Up @@ -90,6 +90,15 @@ class Number(models.Model):
def __unicode__(self):
return unicode(self.num)

# Symmetrical m2m field with a normal field using the reverse accesor name
# ("valid").
class Valid(models.Model):
valid = models.CharField(max_length=10)
parent = models.ManyToManyField('self')

class Meta:
ordering = ['valid']

# Some funky cross-linked models for testing a couple of infinite recursion
# cases.
class X(models.Model):
Expand Down Expand Up @@ -768,5 +777,9 @@ class Child(models.Model):
>>> len(Tag.objects.order_by('parent__name'))
5
Bug #7107 -- this shouldn't create an infinite loop.
>>> Valid.objects.all()
[]
"""}

0 comments on commit 0e692fd

Please sign in to comment.