Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit b118b28bac483133d08eeee5ebc791eb527249c6 1 parent 73d6274
Jacob Kaplan-Moss authored

Showing 1 changed file with 10 additions and 4 deletions. Show diff stats Hide diff stats

  1. 14  django/contrib/contenttypes/models.py
14  django/contrib/contenttypes/models.py
... ...
@@ -1,6 +1,7 @@
1 1
 from django.db import models
2 2
 from django.utils.translation import gettext_lazy as _
3 3
 
  4
+CONTENT_TYPE_CACHE = {}
4 5
 class ContentTypeManager(models.Manager):
5 6
     def get_for_model(self, model):
6 7
         """
@@ -8,10 +9,15 @@ def get_for_model(self, model):
8 9
         ContentType if necessary.
9 10
         """
10 11
         opts = model._meta
11  
-        # The str() is needed around opts.verbose_name because it's a
12  
-        # django.utils.functional.__proxy__ object.
13  
-        ct, created = self.model._default_manager.get_or_create(app_label=opts.app_label,
14  
-            model=opts.object_name.lower(), defaults={'name': str(opts.verbose_name)})
  12
+        key = (opts.app_label, opts.object_name.lower())
  13
+        try:
  14
+            ct = CONTENT_TYPE_CACHE[key]
  15
+        except KeyError:
  16
+            # The str() is needed around opts.verbose_name because it's a
  17
+            # django.utils.functional.__proxy__ object.
  18
+            ct, created = self.model._default_manager.get_or_create(app_label=key[0],
  19
+                model=key[1], defaults={'name': str(opts.verbose_name)})
  20
+            CONTENT_TYPE_CACHE[key] = ct
15 21
         return ct
16 22
 
17 23
 class ContentType(models.Model):

0 notes on commit b118b28

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