summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2009-04-11 13:22:32 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2009-04-11 13:22:32 +0000
commit92824e71028d40009d32fc68d459b0c2242fb7fe (patch)
tree4dda465a583d35a8d1e9483b13b6f334ef78a8c1 /django
parente12e0e18a4a666c22e119990a495c76079110934 (diff)
Fixed #10738 -- Fixed content type values for deferred and proxy models.
Models with deferred fields, or which are proxying for an existing model, now return the same ContentType object as the real model they reflect. Thanks to tomasz.elendt for help with fixing this. git-svn-id: http://code.djangoproject.com/svn/django/trunk@10523 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
-rw-r--r--django/contrib/contenttypes/models.py19
1 files changed, 11 insertions, 8 deletions
diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py
index 4df1edcc75..def7ce6986 100644
--- a/django/contrib/contenttypes/models.py
+++ b/django/contrib/contenttypes/models.py
@@ -7,7 +7,7 @@ class ContentTypeManager(models.Manager):
# Cache to avoid re-looking up ContentType objects all over the place.
# This cache is shared by all the get_for_* methods.
_cache = {}
-
+
def get_for_model(self, model):
"""
Returns the ContentType object for a given model, creating the
@@ -15,22 +15,25 @@ class ContentTypeManager(models.Manager):
for the same model don't hit the database.
"""
opts = model._meta
+ while opts.proxy:
+ model = opts.proxy_for_model
+ opts = model._meta
key = (opts.app_label, opts.object_name.lower())
try:
ct = self.__class__._cache[key]
except KeyError:
- # Load or create the ContentType entry. The smart_unicode() is
+ # Load or create the ContentType entry. The smart_unicode() is
# needed around opts.verbose_name_raw because name_raw might be a
# django.utils.functional.__proxy__ object.
ct, created = self.get_or_create(
app_label = opts.app_label,
- model = opts.object_name.lower(),
+ model = opts.object_name.lower(),
defaults = {'name': smart_unicode(opts.verbose_name_raw)},
)
self._add_to_cache(ct)
-
+
return ct
-
+
def get_for_id(self, id):
"""
Lookup a ContentType by ID. Uses the same shared cache as get_for_model
@@ -44,7 +47,7 @@ class ContentTypeManager(models.Manager):
ct = self.get(pk=id)
self._add_to_cache(ct)
return ct
-
+
def clear_cache(self):
"""
Clear out the content-type cache. This needs to happen during database
@@ -53,7 +56,7 @@ class ContentTypeManager(models.Manager):
this gets called).
"""
self.__class__._cache.clear()
-
+
def _add_to_cache(self, ct):
"""Insert a ContentType into the cache."""
model = ct.model_class()
@@ -66,7 +69,7 @@ class ContentType(models.Model):
app_label = models.CharField(max_length=100)
model = models.CharField(_('python model class name'), max_length=100)
objects = ContentTypeManager()
-
+
class Meta:
verbose_name = _('content type')
verbose_name_plural = _('content types')