Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9546: GenericRelations inherited from base models no longer qu…

…ery using the wrong content type.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10373 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 595c092a293396eab80c6da8654f6705a28c7431 1 parent c6c25ad
Jacob Kaplan-Moss authored April 03, 2009
6  django/contrib/contenttypes/generic.py
@@ -193,9 +193,9 @@ def __get__(self, instance, instance_type=None):
193 193
         rel_model = self.field.rel.to
194 194
         superclass = rel_model._default_manager.__class__
195 195
         RelatedManager = create_generic_related_manager(superclass)
196  
-
  196
+        
197 197
         qn = connection.ops.quote_name
198  
-
  198
+                
199 199
         manager = RelatedManager(
200 200
             model = rel_model,
201 201
             instance = instance,
@@ -203,7 +203,7 @@ def __get__(self, instance, instance_type=None):
203 203
             join_table = qn(self.field.m2m_db_table()),
204 204
             source_col_name = qn(self.field.m2m_column_name()),
205 205
             target_col_name = qn(self.field.m2m_reverse_name()),
206  
-            content_type = ContentType.objects.get_for_model(self.field.model),
  206
+            content_type = ContentType.objects.get_for_model(instance),
207 207
             content_type_field_name = self.field.content_type_field_name,
208 208
             object_id_field_name = self.field.object_id_field_name
209 209
         )
0  tests/regressiontests/generic_relations_regress/__init__.py
No changes.
22  tests/regressiontests/generic_relations_regress/models.py
... ...
@@ -0,0 +1,22 @@
  1
+from django.db import models
  2
+from django.contrib.contenttypes import generic
  3
+from django.contrib.contenttypes.models import ContentType
  4
+
  5
+class Link(models.Model):
  6
+    content_type = models.ForeignKey(ContentType)
  7
+    object_id = models.PositiveIntegerField()
  8
+    content_object = generic.GenericForeignKey()
  9
+
  10
+    def __unicode__(self):
  11
+        return "Link to %s id=%s" % (self.content_type, self.object_id)
  12
+
  13
+class Place(models.Model):
  14
+    name = models.CharField(max_length=100)
  15
+    links = generic.GenericRelation(Link)
  16
+    
  17
+    def __unicode__(self):
  18
+        return "Place: %s" % self.name
  19
+    
  20
+class Restaurant(Place): 
  21
+    def __unicode__(self):
  22
+        return "Restaurant: %s" % self.name
19  tests/regressiontests/generic_relations_regress/tests.py
... ...
@@ -0,0 +1,19 @@
  1
+from django.test import TestCase
  2
+from django.contrib.contenttypes.models import ContentType
  3
+from models import Link, Place, Restaurant
  4
+
  5
+class GenericRelationTests(TestCase):
  6
+    
  7
+    def test_inherited_models_content_type(self):
  8
+        """
  9
+        Test that GenericRelations on inherited classes use the correct content
  10
+        type.
  11
+        """
  12
+        
  13
+        p = Place.objects.create(name="South Park")
  14
+        r = Restaurant.objects.create(name="Chubby's")        
  15
+        l1 = Link.objects.create(content_object=p)
  16
+        l2 = Link.objects.create(content_object=r)
  17
+        self.assertEqual(list(p.links.all()), [l1])
  18
+        self.assertEqual(list(r.links.all()), [l2])
  19
+        

0 notes on commit 595c092

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