View on GitHub

Ann Molly Paul

learn -- code -- inspire

Use multi_db=True for testing with multiple Django dbs

March 16, 2015

When you have configured multiple databases in django, and are testing those models in unit tests, you will notice that the objects create in the models that are part of thenon-default database, tend to not get cleaned up. This messes up other tests if ever you do a counts on the objects created/filtered since the test db gets poluted with the non deleted data.

A quick fix would be to define the def tearDown: to delete all the objects of the model being tested from the non-default model.

However, Django is much smarter than you think. There is a global variable that you can set called multi_db=True which cleans up objects from non-default databases as well!!