summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Kaplan-Moss <jacob@jacobian.org>2007-01-12 15:15:05 +0000
committerJacob Kaplan-Moss <jacob@jacobian.org>2007-01-12 15:15:05 +0000
commitb118b28bac483133d08eeee5ebc791eb527249c6 (patch)
tree84cfb18839d5aee5024d13c8e375116e3cf3f231
parent73d62743e9cc27f09a5e30bcf7b0dbe64d6108de (diff)
Fixed #1717: ContentType.objects.get_for_manager() is now cached for a small performance gain when dealing with content-types regularly. Thanks, Dave St.Germain.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4307 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/contrib/contenttypes/models.py14
1 files changed, 10 insertions, 4 deletions
diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py
index a95748a9a1..3384134cb2 100644
--- a/django/contrib/contenttypes/models.py
+++ b/django/contrib/contenttypes/models.py
@@ -1,6 +1,7 @@
from django.db import models
from django.utils.translation import gettext_lazy as _
+CONTENT_TYPE_CACHE = {}
class ContentTypeManager(models.Manager):
def get_for_model(self, model):
"""
@@ -8,10 +9,15 @@ class ContentTypeManager(models.Manager):
ContentType if necessary.
"""
opts = model._meta
- # The str() is needed around opts.verbose_name because it's a
- # django.utils.functional.__proxy__ object.
- ct, created = self.model._default_manager.get_or_create(app_label=opts.app_label,
- model=opts.object_name.lower(), defaults={'name': str(opts.verbose_name)})
+ key = (opts.app_label, opts.object_name.lower())
+ try:
+ ct = CONTENT_TYPE_CACHE[key]
+ except KeyError:
+ # The str() is needed around opts.verbose_name because it's a
+ # django.utils.functional.__proxy__ object.
+ ct, created = self.model._default_manager.get_or_create(app_label=key[0],
+ model=key[1], defaults={'name': str(opts.verbose_name)})
+ CONTENT_TYPE_CACHE[key] = ct
return ct
class ContentType(models.Model):