Skip to content

Fix database access warning during app initialization with Django 5#1288

Open
gagelarsen wants to merge 2 commits into
mainfrom
fix-db-access-warning-1060
Open

Fix database access warning during app initialization with Django 5#1288
gagelarsen wants to merge 2 commits into
mainfrom
fix-db-access-warning-1060

Conversation

@gagelarsen

Copy link
Copy Markdown
Contributor

Description

This merge request addresses the RuntimeWarning: Accessing the database during app initialization is discouraged warning that Django 5 emits when starting the development server with tethys manage start. There were three sources of database access while apps.ready is False:

  1. tethys_apps/admin.py built the dynamic group admin form at import time (admin autodiscovery runs during app initialization), iterating TethysApp.objects.all() and querying GroupObjectPermission.
  2. App harvesting in TethysAppsConfig.ready() syncs apps/extensions (and cookies) with the database by design.
  3. TethysQuotasConfig.ready() syncs resource quota handlers with the database by design.

Changes Made to Code

  • tethys_apps/admin.py: CustomGroup now creates the GOP app access form lazily in get_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 default GroupAdmin form. As a side benefit, the form now reflects the currently installed apps on every request.
  • tethys_apps/apps.py and tethys_quotas/apps.py: the database syncing done in ready() is intentional, so Django's app-initialization RuntimeWarning is suppressed around exactly those calls (scoped with warnings.catch_warnings() and matched by message + category).
  • Added/updated unit tests: registration executes zero queries (CaptureQueriesContext), get_form() builds the dynamic per-app fields and degrades gracefully on ProgrammingError, and both ready() methods suppress the warning (skipped on Django < 5.0 where the warning doesn't exist).

Verified end-to-end: tethys manage start previously 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

  • The suppression is scoped tightly: only RuntimeWarnings matching Django's app-initialization message, only around the intentional sync calls, and filter state is restored afterward.

Quality Checks

  • At least one new test has been written for new code
  • New code has 100% test coverage
  • Code has been formatted with Black
  • Code has been linted with flake8
  • Docstrings for new methods have been added
  • The documentation has been updated appropriately

🤖 Generated with Claude Code

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>
@coveralls

coveralls commented Jul 14, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 100.0%. remained the same — fix-db-access-warning-1060 into main

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Warning during tethys manage start when using Django 5.0

2 participants