ChannelsLiveServerTestCase keeps old ContentType cache between tests
Author: johanneswilmCreated Dec 11, 2025Updated Dec 17, 2025
When running multiple ChannelsLiveServerTestCase tests, the id values of the contenttypes change between test runs, yet the cache is not emptied. So ContentType.get_for_model(X) will get the instance from a previous test.
The only work-around I found was to add the following within the django view itself:
if 'test' in sys.argv:
ContentType.objects.clear_cache()It is not enough to add this in the setUp or tearDown methods of the test, as the test runs in a separate thread and therefore has a separate cache. If someone can tell me how to, I'd very much like to move this to to the test code.
Tested with Django 5.2.9 and Django Channels 4.3.2.
Source: django/channels