Fix database access warning during app initialization with Django 5#1288
Open
gagelarsen wants to merge 2 commits into
Open
Fix database access warning during app initialization with Django 5#1288gagelarsen wants to merge 2 commits into
gagelarsen wants to merge 2 commits into
Conversation
Django 5 warns when the database is accessed during app initialization. Tethys triggered this from three places at startup: - tethys_apps/admin.py built the dynamic group admin form (which queries TethysApp and GroupObjectPermission) at import time. The form is now created lazily in CustomGroup.get_form() so no queries run on import. - App harvesting in TethysAppsConfig.ready() and quota handler syncing in TethysQuotasConfig.ready() query the database by design, so the warning is suppressed around those calls. Fixes #1060 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
With the GOP form now created lazily in get_form(), registering the custom Group admin no longer touches the database, so the DB-error handler around it is dead code (and was flagged by coveralls). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
6 tasks
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.
Description
This merge request addresses the
RuntimeWarning: Accessing the database during app initialization is discouragedwarning that Django 5 emits when starting the development server withtethys manage start. There were three sources of database access whileapps.readyisFalse:tethys_apps/admin.pybuilt the dynamic group admin form at import time (admin autodiscovery runs during app initialization), iteratingTethysApp.objects.all()and queryingGroupObjectPermission.TethysAppsConfig.ready()syncs apps/extensions (and cookies) with the database by design.TethysQuotasConfig.ready()syncs resource quota handlers with the database by design.Changes Made to Code
tethys_apps/admin.py:CustomGroupnow creates the GOP app access form lazily inget_form()instead of at registration/import time (as suggested in the issue comments), so no queries run during app initialization. If the database isn't migrated yet, it logs a warning and falls back to the defaultGroupAdminform. As a side benefit, the form now reflects the currently installed apps on every request.tethys_apps/apps.pyandtethys_quotas/apps.py: the database syncing done inready()is intentional, so Django's app-initializationRuntimeWarningis suppressed around exactly those calls (scoped withwarnings.catch_warnings()and matched by message + category).CaptureQueriesContext),get_form()builds the dynamic per-app fields and degrades gracefully onProgrammingError, and bothready()methods suppress the warning (skipped on Django < 5.0 where the warning doesn't exist).Verified end-to-end:
tethys manage startpreviously displayed the warning (45 init-time queries flagged with-W always); with this change it starts with zero warnings, and the Group admin add/change/save pages work with all dynamic per-app fields.Related PRs, Issues, and Discussions
Additional Notes
RuntimeWarnings matching Django's app-initialization message, only around the intentional sync calls, and filter state is restored afterward.Quality Checks
🤖 Generated with Claude Code