2
Make sure that the content type cache (see ContentTypeManager) works correctly.
3
Lookups for a particular content type -- by model or by ID -- should hit the
4
database only on the first lookup.
6
First, let's make sure we're dealing with a blank slate (and that DEBUG is on so
7
that queries get logged)::
9
>>> from django.conf import settings
10
>>> settings.DEBUG = True
12
>>> from django.contrib.contenttypes.models import ContentType
13
>>> ContentType.objects.clear_cache()
15
>>> from django import db
16
>>> db.reset_queries()
18
At this point, a lookup for a ContentType should hit the DB::
20
>>> ContentType.objects.get_for_model(ContentType)
21
<ContentType: content type>
23
>>> len(db.connection.queries)
26
A second hit, though, won't hit the DB, nor will a lookup by ID::
28
>>> ct = ContentType.objects.get_for_model(ContentType)
29
>>> len(db.connection.queries)
31
>>> ContentType.objects.get_for_id(ct.id)
32
<ContentType: content type>
33
>>> len(db.connection.queries)
36
Once we clear the cache, another lookup will again hit the DB::
38
>>> ContentType.objects.clear_cache()
39
>>> ContentType.objects.get_for_model(ContentType)
40
<ContentType: content type>
41
>>> len(db.connection.queries)
44
Don't forget to reset DEBUG!
46
>>> settings.DEBUG = False
b'\\ No newline at end of file'