Author: jacob
Date: 2007-01-12 09:15:05 -0600 (Fri, 12 Jan 2007)
New Revision: 4307

Modified:
   django/trunk/django/contrib/contenttypes/models.py
Log:
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.


Modified: django/trunk/django/contrib/contenttypes/models.py
===================================================================
--- django/trunk/django/contrib/contenttypes/models.py  2007-01-11 00:04:27 UTC 
(rev 4306)
+++ django/trunk/django/contrib/contenttypes/models.py  2007-01-12 15:15:05 UTC 
(rev 4307)
@@ -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 @@
         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):


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to