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 Alexander Artemenko, 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  
    11from django.db import models
    22from django.utils.translation import ugettext_lazy as _
    33from django.utils.encoding import smart_unicode
     4from django.db.models.query import CollectedObjects
     5from django.db.models import Model
    46
    57class ContentTypeManager(models.Manager):
    68
    class ContentTypeManager(models.Manager):  
    1416        ContentType if necessary. Lookups are cached so that subsequent lookups
    1517        for the same model don't hit the database.
    1618        """
     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
    1725        opts = model._meta
    1826        key = (opts.app_label, opts.object_name.lower())
    1927        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::  
    4444Don't forget to reset DEBUG!
    4545
    4646    >>> settings.DEBUG = False
    47 """
    48  No newline at end of file
     47"""
     48
     49import unittest
     50from django.db import models
     51from models import ContentType
     52
     53class Parent(models.Model): pass
     54class Child(Parent): pass
     55class Child2(Parent): pass
     56
     57
     58class 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
Back to Top