diff options
| author | Julien Phalip <jphalip@gmail.com> | 2012-02-11 09:55:54 +0000 |
|---|---|---|
| committer | Julien Phalip <jphalip@gmail.com> | 2012-02-11 09:55:54 +0000 |
| commit | 130e7ab617d746291629de6a5a1fbb5ea9cd3fe5 (patch) | |
| tree | b1d7e57ca3156db4d9b1728f7a7e1e97a7eb2968 /django | |
| parent | 995f7a16a8bf539caf076a7744441a383e9a38b2 (diff) | |
Fixed #17256 -- Ensured that content types get cached when retrieved by natural key. Thanks, defaultwombat and charettes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17502 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/contenttypes/models.py | 1 | ||||
| -rw-r--r-- | django/contrib/contenttypes/tests.py | 18 |
2 files changed, 17 insertions, 2 deletions
diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py index 1bb07e0d52..6d919b4237 100644 --- a/django/contrib/contenttypes/models.py +++ b/django/contrib/contenttypes/models.py @@ -13,6 +13,7 @@ class ContentTypeManager(models.Manager): 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): diff --git a/django/contrib/contenttypes/tests.py b/django/contrib/contenttypes/tests.py index f2a338e1d4..3b7906c812 100644 --- a/django/contrib/contenttypes/tests.py +++ b/django/contrib/contenttypes/tests.py @@ -51,8 +51,8 @@ class ContentTypesTests(TestCase): 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 @@ class ContentTypesTests(TestCase): 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): |
