diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py
index 1bb07e0..6d919b4 100644
a
|
b
|
def get_by_natural_key(self, app_label, model):
|
13 | 13 | ct = self.__class__._cache[self.db][(app_label, model)] |
14 | 14 | except KeyError: |
15 | 15 | ct = self.get(app_label=app_label, model=model) |
| 16 | self._add_to_cache(self.db, ct) |
16 | 17 | return ct |
17 | 18 | |
18 | 19 | def _get_opts(self, model): |
diff --git a/django/contrib/contenttypes/tests.py b/django/contrib/contenttypes/tests.py
index f2a338e..3b7906c 100644
a
|
b
|
def tearDown(self):
|
51 | 51 | def test_lookup_cache(self): |
52 | 52 | """ |
53 | 53 | Make sure that the content type cache (see ContentTypeManager) |
54 | | works correctly. Lookups for a particular content type -- by model or |
55 | | by ID -- should hit the database only on the first lookup. |
| 54 | works correctly. Lookups for a particular content type -- by model, ID |
| 55 | or natural key -- should hit the database only on the first lookup. |
56 | 56 | """ |
57 | 57 | |
58 | 58 | # At this point, a lookup for a ContentType should hit the DB |
… |
… |
def test_lookup_cache(self):
|
60 | 60 | ContentType.objects.get_for_model(ContentType) |
61 | 61 | |
62 | 62 | # A second hit, though, won't hit the DB, nor will a lookup by ID |
| 63 | # or natural key |
63 | 64 | with self.assertNumQueries(0): |
64 | 65 | ct = ContentType.objects.get_for_model(ContentType) |
65 | 66 | with self.assertNumQueries(0): |
66 | 67 | ContentType.objects.get_for_id(ct.id) |
| 68 | with self.assertNumQueries(0): |
| 69 | ContentType.objects.get_by_natural_key('contenttypes', |
| 70 | 'contenttype') |
67 | 71 | |
68 | 72 | # Once we clear the cache, another lookup will again hit the DB |
69 | 73 | ContentType.objects.clear_cache() |
70 | 74 | with self.assertNumQueries(1): |
71 | 75 | ContentType.objects.get_for_model(ContentType) |
72 | 76 | |
| 77 | # The same should happen with a lookup by natural key |
| 78 | ContentType.objects.clear_cache() |
| 79 | with self.assertNumQueries(1): |
| 80 | ContentType.objects.get_by_natural_key('contenttypes', |
| 81 | 'contenttype') |
| 82 | # And a second hit shouldn't hit the DB |
| 83 | with self.assertNumQueries(0): |
| 84 | ContentType.objects.get_by_natural_key('contenttypes', |
| 85 | 'contenttype') |
| 86 | |
73 | 87 | def test_get_for_models_empty_cache(self): |
74 | 88 | # Empty cache. |
75 | 89 | with self.assertNumQueries(1): |
diff --git a/tests/regressiontests/comment_tests/tests/templatetag_tests.py b/tests/regressiontests/comment_tests/tests/templatetag_tests.py
index de39767..0b036ce 100644
a
|
b
|
def testNumberQueries(self):
|
130 | 130 | with self.assertNumQueries(4): |
131 | 131 | self.testRenderCommentListFromObject() |
132 | 132 | |
133 | | # Force the CT to be cached |
134 | | ct = ContentType.objects.get_for_model(Article) |
| 133 | # CT's should be cached |
135 | 134 | with self.assertNumQueries(3): |
136 | 135 | self.testRenderCommentListFromObject() |
137 | 136 | |
… |
… |
def testNumberQueries(self):
|
141 | 140 | with self.assertNumQueries(4): |
142 | 141 | self.verifyGetCommentList() |
143 | 142 | |
144 | | ct = ContentType.objects.get_for_model(Author) |
145 | 143 | with self.assertNumQueries(3): |
146 | 144 | self.verifyGetCommentList() |
147 | 145 | |
… |
… |
def testNumberQueries(self):
|
151 | 149 | with self.assertNumQueries(3): |
152 | 150 | self.testRenderCommentForm() |
153 | 151 | |
154 | | ct = ContentType.objects.get_for_model(Article) |
155 | 152 | with self.assertNumQueries(2): |
156 | 153 | self.testRenderCommentForm() |
157 | 154 | |
… |
… |
def testNumberQueries(self):
|
161 | 158 | with self.assertNumQueries(3): |
162 | 159 | self.testGetCommentForm() |
163 | 160 | |
164 | | ct = ContentType.objects.get_for_model(Article) |
165 | 161 | with self.assertNumQueries(2): |
166 | 162 | self.testGetCommentForm() |
167 | 163 | |
… |
… |
def testNumberQueries(self):
|
171 | 167 | with self.assertNumQueries(3): |
172 | 168 | self.verifyGetCommentCount() |
173 | 169 | |
174 | | ct = ContentType.objects.get_for_model(Article) |
175 | 170 | with self.assertNumQueries(2): |
176 | 171 | self.verifyGetCommentCount() |