Author: mtredinnick
Date: 2007-03-07 21:21:35 -0600 (Wed, 07 Mar 2007)
New Revision: 4676

Modified:
   django/trunk/django/db/models/related.py
   django/trunk/tests/regressiontests/many_to_one_regress/models.py
Log:
Fixed #1839, #2415, #2536 -- Fixed a generated name clash that was common in
self-referential and circular relations. A lot of community debugging went into
this fix, so thanks to [EMAIL PROTECTED], 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).


Modified: django/trunk/django/db/models/related.py
===================================================================
--- django/trunk/django/db/models/related.py    2007-03-07 23:22:37 UTC (rev 
4675)
+++ django/trunk/django/db/models/related.py    2007-03-08 03:21:35 UTC (rev 
4676)
@@ -1,7 +1,7 @@
 class BoundRelatedObject(object):
     def __init__(self, related_object, field_mapping, original):
         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):
         raise NotImplementedError
@@ -16,7 +16,7 @@
         self.opts = model._meta
         self.field = field
         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()
 
     def flatten_data(self, follow, obj=None):

Modified: django/trunk/tests/regressiontests/many_to_one_regress/models.py
===================================================================
--- django/trunk/tests/regressiontests/many_to_one_regress/models.py    
2007-03-07 23:22:37 UTC (rev 4675)
+++ django/trunk/tests/regressiontests/many_to_one_regress/models.py    
2007-03-08 03:21:35 UTC (rev 4676)
@@ -1,13 +1,34 @@
 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):
     second = models.IntegerField()
 
 class Second(models.Model):
     first = models.ForeignKey(First, related_name = 'the_first')
 
-# 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).
+# Protect against repetition of #1839, #2415 and #2536.
+class Third(models.Model):
+    name = models.CharField(maxlength=20)
+    third = models.ForeignKey('self', null=True, related_name='child_set')
 
-__test__ = {'API_TESTS':""}
+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':"""
+>>> 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>
+"""}


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to