Ticket #9317: 0001-Right-content-type-if-parent-object-if-given-and-ac.patch
File 0001-Right-content-type-if-parent-object-if-given-and-ac.patch, 2.7 KB (added by , 16 years ago) |
---|
-
django/contrib/contenttypes/models.py
diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py index 4df1edc..3497b11 100644
a b 1 1 from django.db import models 2 2 from django.utils.translation import ugettext_lazy as _ 3 3 from django.utils.encoding import smart_unicode 4 from django.db.models.query import CollectedObjects 5 from django.db.models import Model 4 6 5 7 class ContentTypeManager(models.Manager): 6 8 … … class ContentTypeManager(models.Manager): 14 16 ContentType if necessary. Lookups are cached so that subsequent lookups 15 17 for the same model don't hit the database. 16 18 """ 19 if isinstance(model, Model): 20 # model inheritance support 21 sub_objects = CollectedObjects() 22 model._collect_sub_objects(sub_objects) 23 model = sub_objects.items()[0][1].values()[0] 24 17 25 opts = model._meta 18 26 key = (opts.app_label, opts.object_name.lower()) 19 27 try: -
django/contrib/contenttypes/tests.py
diff --git a/django/contrib/contenttypes/tests.py b/django/contrib/contenttypes/tests.py index 148edfc..92fba74 100644
a b Once we clear the cache, another lookup will again hit the DB:: 44 44 Don't forget to reset DEBUG! 45 45 46 46 >>> settings.DEBUG = False 47 """ 48 No newline at end of file 47 """ 48 49 import unittest 50 from django.db import models 51 from models import ContentType 52 53 class Parent(models.Model): pass 54 class Child(Parent): pass 55 class Child2(Parent): pass 56 57 58 class Inheritance(unittest.TestCase): 59 def setUp(self): 60 Parent.objects.all().delete() 61 62 def testInheritance(self): 63 child = Child() 64 child.save() 65 66 parent = Parent.objects.get(id=child.parent_ptr_id) 67 68 parent_ct = ContentType.objects.get_for_model(parent) 69 child_ct = ContentType.objects.get_for_model(child) 70 71 self.assertEqual(parent_ct, child_ct) 72 73 def testInheritanceWithMultipleChilds(self): 74 child1 = Child() 75 child1.save() 76 77 child2 = Child2() 78 child2.save() 79 80 self.assertEqual(2, Parent.objects.all().count()) 81 82 parent1 = Parent.objects.get(id=child1.parent_ptr_id) 83 parent1_ct = ContentType.objects.get_for_model(parent1) 84 85 parent2 = Parent.objects.get(id=child2.parent_ptr_id) 86 parent2_ct = ContentType.objects.get_for_model(parent2) 87 88 child1_ct = ContentType.objects.get_for_model(child1) 89 child2_ct = ContentType.objects.get_for_model(child2) 90 91 self.assertNotEqual(child1_ct, child2_ct) 92 self.assertEqual(parent1_ct, child1_ct) 93 self.assertEqual(parent2_ct, child2_ct) 94