Skip to content

Commit

Permalink
Fixed #1839, #2415, #2536 -- Fixed a generated name clash that was co…
Browse files Browse the repository at this point in the history
…mmon in

self-referential and circular relations. A lot of community debugging went into
this fix, so thanks to bmurdock@gmail.com, Marek Kubica, ramiro, Michael
Radziej (the last two giving test cases showing the problem) and James Bennett
(who did the hard work to actually diagnose the true problem and fix it).


git-svn-id: http://code.djangoproject.com/svn/django/trunk@4676 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
malcolmt committed Mar 8, 2007
1 parent 98b2e0a commit 43809de
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
4 changes: 2 additions & 2 deletions django/db/models/related.py
@@ -1,7 +1,7 @@
class BoundRelatedObject(object): class BoundRelatedObject(object):
def __init__(self, related_object, field_mapping, original): def __init__(self, related_object, field_mapping, original):
self.relation = related_object self.relation = related_object
self.field_mappings = field_mapping[related_object.opts.module_name] self.field_mappings = field_mapping[related_object.name]


def template_name(self): def template_name(self):
raise NotImplementedError raise NotImplementedError
Expand All @@ -16,7 +16,7 @@ def __init__(self, parent_model, model, field):
self.opts = model._meta self.opts = model._meta
self.field = field self.field = field
self.edit_inline = field.rel.edit_inline self.edit_inline = field.rel.edit_inline
self.name = self.opts.module_name self.name = '%s_%s' % (self.opts.app_label, self.opts.module_name)
self.var_name = self.opts.object_name.lower() self.var_name = self.opts.object_name.lower()


def flatten_data(self, follow, obj=None): def flatten_data(self, follow, obj=None):
Expand Down
29 changes: 25 additions & 4 deletions tests/regressiontests/many_to_one_regress/models.py
@@ -1,13 +1,34 @@
from django.db import models from django.db import models


# If ticket #1578 ever slips back in, these models will not be able to be
# created (the field names being lower-cased versions of their opposite
# classes is important here).

class First(models.Model): class First(models.Model):
second = models.IntegerField() second = models.IntegerField()


class Second(models.Model): class Second(models.Model):
first = models.ForeignKey(First, related_name = 'the_first') first = models.ForeignKey(First, related_name = 'the_first')


# If ticket #1578 ever slips back in, these models will not be able to be # Protect against repetition of #1839, #2415 and #2536.
# created (the field names being lower-cased versions of their opposite class Third(models.Model):
# classes is important here). name = models.CharField(maxlength=20)
third = models.ForeignKey('self', null=True, related_name='child_set')

class Parent(models.Model):
name = models.CharField(maxlength=20)
bestchild = models.ForeignKey('Child', null=True, related_name='favored_by')

class Child(models.Model):
name = models.CharField(maxlength=20)
parent = models.ForeignKey(Parent)



__test__ = {'API_TESTS':""} __test__ = {'API_TESTS':"""
>>> Third.AddManipulator().save(dict(id='3', name='An example', another=None))
<Third: Third object>
>>> parent = Parent(name = 'fred')
>>> parent.save()
>>> Child.AddManipulator().save(dict(name='bam-bam', parent=parent.id))
<Child: Child object>
"""}

0 comments on commit 43809de

Please sign in to comment.