I'm running the tests with --parallel and want to add some object to every database that is created (for each process).
currently, I have a CustomTestRunner which inherit from DiscoverRunner.
In that class, I'm overriding setup_databases method.
after calling super().setup_databases(), I'm making a change in the db (like Users.objects.create(....))
The changes occur only in one of the databases, But I want that change to be in all of them.
How can I achieve that? My Custom Test Runner
You can register the code to run on the post-migrate signal. For example:
if settings.TESTING_MODE:
@receiver(post_migrate)
def modify_database(*, sender: AppConfig, **kwargs):
# The signal is emitted once for every app’s migrations,
# so if you only want it to run once, check for your app’s
# migrations to finish.
if sender.name != YourAppConfig.name:
return
# Your code here.
...
This will run after the first database is created, but before the others get mirrored from it, so whatever you populate into that database will be reflected in all of them.