I'm trying to use the django's contenttypes library and I noticed something weird.
The following line:
for content_type in ContentType.objects.all():
print "%s: %s" % (content_type.id, content_type.model)
prints different ids for each of my models depending on when it's called.
When I run the test suite, first it prints this:
1: logentry
2: permission
3: group
4: user
5: contenttype
6: session
12: tagtagrule
15: concepttag
16: difficultytag
17: questiontypetag
18: questionformattag
19: sharedassetconcepttag
20: curriculumtag
21: objectposition
22: usermediaimage
23: objecttag
But then after this message:
Creating test database for alias 'default'...
It seems to reindex everything and prints this:
1: logentry
2: permission
3: group
4: user
5: contenttype
6: session
7: concepttag
8: difficultytag
9: questiontypetag
10: questionformattag
11: sharedassetconcepttag
12: curriculumtag
13: tagtagrule
14: objecttag
What's going on here?
it seems like in the first instance your print code is running too early in the process, before the default db connection has been swapped for the test database by Django test runner...
after the Creating test database for alias 'default'...
message it means Django has created the separate database that will be used by all your tests
then when your print code runs again you see the content as in the test db
the different order and discontinuous ids of the content types in your default db reflect the order in which you added (and removed) models from your project during development
the continuous id sequence seen in the test db reflects the fact that it looked at all your current models and created content-types for them in a fresh empty db
You should definitely not expect your content types to have specific id values in your tests (or your project code). You should retrieve content types by querying them by model
and app_label
.