Author: jbronn
Date: 2010-01-22 13:43:14 -0600 (Fri, 22 Jan 2010)
New Revision: 12277

Modified:
   django/branches/releases/1.1.X/django/contrib/contenttypes/generic.py
   
django/branches/releases/1.1.X/tests/regressiontests/generic_relations_regress/models.py
   
django/branches/releases/1.1.X/tests/regressiontests/generic_relations_regress/tests.py
Log:
[1.1.X] Fixed #12664 -- Fixed `GenericRelation.m2m_reverse_name` to return the 
correct pk column name.

Backport of r12276 from trunk.


Modified: django/branches/releases/1.1.X/django/contrib/contenttypes/generic.py
===================================================================
--- django/branches/releases/1.1.X/django/contrib/contenttypes/generic.py       
2010-01-22 18:39:48 UTC (rev 12276)
+++ django/branches/releases/1.1.X/django/contrib/contenttypes/generic.py       
2010-01-22 19:43:14 UTC (rev 12277)
@@ -131,7 +131,7 @@
         return self.object_id_field_name
 
     def m2m_reverse_name(self):
-        return self.model._meta.pk.column
+        return self.rel.to._meta.pk.column
 
     def contribute_to_class(self, cls, name):
         super(GenericRelation, self).contribute_to_class(cls, name)

Modified: 
django/branches/releases/1.1.X/tests/regressiontests/generic_relations_regress/models.py
===================================================================
--- 
django/branches/releases/1.1.X/tests/regressiontests/generic_relations_regress/models.py
    2010-01-22 18:39:48 UTC (rev 12276)
+++ 
django/branches/releases/1.1.X/tests/regressiontests/generic_relations_regress/models.py
    2010-01-22 19:43:14 UTC (rev 12277)
@@ -13,10 +13,30 @@
 class Place(models.Model):
     name = models.CharField(max_length=100)
     links = generic.GenericRelation(Link)
-    
+
     def __unicode__(self):
         return "Place: %s" % self.name
-    
-class Restaurant(Place): 
+
+class Restaurant(Place):
     def __unicode__(self):
-        return "Restaurant: %s" % self.name
\ No newline at end of file
+        return "Restaurant: %s" % self.name
+
+class Address(models.Model):
+    street = models.CharField(max_length=80)
+    city = models.CharField(max_length=50)
+    state = models.CharField(max_length=2)
+    zipcode = models.CharField(max_length=5)
+    content_type = models.ForeignKey(ContentType)
+    object_id = models.PositiveIntegerField()
+    content_object = generic.GenericForeignKey()
+
+    def __unicode__(self):
+        return '%s %s, %s %s' % (self.street, self.city, self.state, 
self.zipcode)
+
+class Person(models.Model):
+    account = models.IntegerField(primary_key=True)
+    name = models.CharField(max_length=128)
+    addresses = generic.GenericRelation(Address)
+
+    def __unicode__(self):
+        return self.name

Modified: 
django/branches/releases/1.1.X/tests/regressiontests/generic_relations_regress/tests.py
===================================================================
--- 
django/branches/releases/1.1.X/tests/regressiontests/generic_relations_regress/tests.py
     2010-01-22 18:39:48 UTC (rev 12276)
+++ 
django/branches/releases/1.1.X/tests/regressiontests/generic_relations_regress/tests.py
     2010-01-22 19:43:14 UTC (rev 12277)
@@ -1,19 +1,32 @@
 from django.test import TestCase
 from django.contrib.contenttypes.models import ContentType
-from models import Link, Place, Restaurant
+from models import Link, Place, Restaurant, Person, Address
 
 class GenericRelationTests(TestCase):
-    
+
     def test_inherited_models_content_type(self):
         """
         Test that GenericRelations on inherited classes use the correct content
         type.
         """
-        
+
         p = Place.objects.create(name="South Park")
-        r = Restaurant.objects.create(name="Chubby's")        
+        r = Restaurant.objects.create(name="Chubby's")
         l1 = Link.objects.create(content_object=p)
         l2 = Link.objects.create(content_object=r)
         self.assertEqual(list(p.links.all()), [l1])
         self.assertEqual(list(r.links.all()), [l2])
-        
\ No newline at end of file
+
+    def test_reverse_relation_pk(self):
+        """
+        Test that the correct column name is used for the primary key on the
+        originating model of a query.  See #12664.
+        """
+        p = Person.objects.create(account=23, name='Chef')
+        a = Address.objects.create(street='123 Anywhere Place',
+                                   city='Conifer', state='CO',
+                                   zipcode='80433', content_object=p)
+
+        qs = Person.objects.filter(addresses__zipcode='80433')
+        self.assertEqual(1, qs.count())
+        self.assertEqual('Chef', qs[0].name)

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