Author: julien
Date: 2012-02-11 01:55:54 -0800 (Sat, 11 Feb 2012)
New Revision: 17502
Modified:
django/trunk/django/contrib/contenttypes/models.py
django/trunk/django/contrib/contenttypes/tests.py
django/trunk/tests/regressiontests/comment_tests/tests/templatetag_tests.py
Log:
Fixed #17256 -- Ensured that content types get cached when retrieved by natural
key. Thanks, defaultwombat and charettes.
Modified: django/trunk/django/contrib/contenttypes/models.py
===================================================================
--- django/trunk/django/contrib/contenttypes/models.py 2012-02-11 09:31:18 UTC
(rev 17501)
+++ django/trunk/django/contrib/contenttypes/models.py 2012-02-11 09:55:54 UTC
(rev 17502)
@@ -13,6 +13,7 @@
ct = self.__class__._cache[self.db][(app_label, model)]
except KeyError:
ct = self.get(app_label=app_label, model=model)
+ self._add_to_cache(self.db, ct)
return ct
def _get_opts(self, model):
Modified: django/trunk/django/contrib/contenttypes/tests.py
===================================================================
--- django/trunk/django/contrib/contenttypes/tests.py 2012-02-11 09:31:18 UTC
(rev 17501)
+++ django/trunk/django/contrib/contenttypes/tests.py 2012-02-11 09:55:54 UTC
(rev 17502)
@@ -51,8 +51,8 @@
def test_lookup_cache(self):
"""
Make sure that the content type cache (see ContentTypeManager)
- works correctly. Lookups for a particular content type -- by model or
- by ID -- should hit the database only on the first lookup.
+ works correctly. Lookups for a particular content type -- by model, ID
+ or natural key -- should hit the database only on the first lookup.
"""
# At this point, a lookup for a ContentType should hit the DB
@@ -60,16 +60,30 @@
ContentType.objects.get_for_model(ContentType)
# A second hit, though, won't hit the DB, nor will a lookup by ID
+ # or natural key
with self.assertNumQueries(0):
ct = ContentType.objects.get_for_model(ContentType)
with self.assertNumQueries(0):
ContentType.objects.get_for_id(ct.id)
+ with self.assertNumQueries(0):
+ ContentType.objects.get_by_natural_key('contenttypes',
+ 'contenttype')
# Once we clear the cache, another lookup will again hit the DB
ContentType.objects.clear_cache()
with self.assertNumQueries(1):
ContentType.objects.get_for_model(ContentType)
+ # The same should happen with a lookup by natural key
+ ContentType.objects.clear_cache()
+ with self.assertNumQueries(1):
+ ContentType.objects.get_by_natural_key('contenttypes',
+ 'contenttype')
+ # And a second hit shouldn't hit the DB
+ with self.assertNumQueries(0):
+ ContentType.objects.get_by_natural_key('contenttypes',
+ 'contenttype')
+
def test_get_for_models_empty_cache(self):
# Empty cache.
with self.assertNumQueries(1):
Modified:
django/trunk/tests/regressiontests/comment_tests/tests/templatetag_tests.py
===================================================================
--- django/trunk/tests/regressiontests/comment_tests/tests/templatetag_tests.py
2012-02-11 09:31:18 UTC (rev 17501)
+++ django/trunk/tests/regressiontests/comment_tests/tests/templatetag_tests.py
2012-02-11 09:55:54 UTC (rev 17502)
@@ -130,8 +130,7 @@
with self.assertNumQueries(4):
self.testRenderCommentListFromObject()
- # Force the CT to be cached
- ct = ContentType.objects.get_for_model(Article)
+ # CT's should be cached
with self.assertNumQueries(3):
self.testRenderCommentListFromObject()
@@ -141,7 +140,6 @@
with self.assertNumQueries(4):
self.verifyGetCommentList()
- ct = ContentType.objects.get_for_model(Author)
with self.assertNumQueries(3):
self.verifyGetCommentList()
@@ -151,7 +149,6 @@
with self.assertNumQueries(3):
self.testRenderCommentForm()
- ct = ContentType.objects.get_for_model(Article)
with self.assertNumQueries(2):
self.testRenderCommentForm()
@@ -161,7 +158,6 @@
with self.assertNumQueries(3):
self.testGetCommentForm()
- ct = ContentType.objects.get_for_model(Article)
with self.assertNumQueries(2):
self.testGetCommentForm()
@@ -171,6 +167,5 @@
with self.assertNumQueries(3):
self.verifyGetCommentCount()
- ct = ContentType.objects.get_for_model(Article)
with self.assertNumQueries(2):
self.verifyGetCommentCount()
--
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.