Fix: Resolve Django 5.x database access during app initialization warning - #44
Merged
Conversation
Owner
|
@ricosalomar looks great -- generated some unit tests to cover some of the code (we never had any for |
peterfarrell
approved these changes
Jun 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: Resolve Django 5.x database access during app initialization warning
1. Root Cause & Context
In Django 5.0+, a new
RuntimeWarningwas introduced to discourage database operations during the application registry population phase (insideAppConfig.ready()or during module imports):In
django-heralder, theready()method ofHeraldConfigcalledNotification.objects.get_or_create(...)to automatically discover and register notification classes. This triggered the warning and caused issues such as potential connection deadlocks in some test suites and startup warning noise.2. Solution (Signal-Based Registration)
This PR implements an idiomatic, safe, and self-contained adjustment inside
src/herald/apps.py:post_migrate: We connected the notification auto-discovery database registration loop to Django'spost_migratesignal instead of running it directly insideready().RuntimeWarningwithout silencing other system warnings.tests/test_usernotifications.py, we updated thesetUp()method to useget_or_create()instead of.save()when instantiatingNotificationtest records. Since test environments run migrations and dispatchpost_migratesignals automatically, the notifications are already cleanly pre-registered in the test DB, avoiding potential uniquenessIntegrityErrors.These changes are fully backward-compatible and have zero negative performance impact.
Verification & Testing Plan
PYTHONPATH=src .venv/bin/python manage.py test