Skip to content

fix(forms): avoid DB access at import time in Import/ReImport forms#15233

Open
Maffooch wants to merge 1 commit into
bugfixfrom
fix/no-db-access-at-import-finding-groups
Open

fix(forms): avoid DB access at import time in Import/ReImport forms#15233
Maffooch wants to merge 1 commit into
bugfixfrom
fix/no-db-access-at-import-finding-groups

Conversation

@Maffooch

@Maffooch Maffooch commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

[sc-13664]

What

ImportScanForm and ReImportScanForm declared their finding-group fields (group_by, create_finding_groups_for_all_findings) in the class body, guarded by is_finding_groups_enabled(). That helper reads a DB-backed system setting, so simply importing dojo.forms issued a SELECT ... FROM system_settings.

When the module is imported during app initialization (e.g. from an AppConfig.ready() hook), Django emits:

RuntimeWarning: Accessing the database during app initialization is discouraged.
To fix this warning, avoid executing queries in AppConfig.ready() or when your
app modules are imported.

Fix

Move the conditional field construction from the class body into each form's __init__ (after super().__init__()), so importing the module never touches the database.

Behavior is unchanged:

  • Instances still gain group_by / create_finding_groups_for_all_findings when finding groups are enabled.
  • The fields keep their original position (they were the last declared fields; adding them in __init__ appends them last).
  • The existing empty-default-choice insertion (if "group_by" in self.fields:) still runs.

Evaluating the setting per-instance rather than once at import is also slightly more correct: a change to the setting is picked up without a process restart.

Testing

  • base_fields no longer contains the fields; a fresh ImportScanForm() / ReImportScanForm() instance still has them (with the ("", "---------") default first) when finding groups are enabled.
  • python manage.py test unittests.test_importers_importer.TestDojoImporterBatchPushToJira — passes (grouping path).
  • ruff --config ruff.toml dojo/forms.py — clean.

Note

Other spots use the same import-time anti-pattern (the FindingFilter FilterSet classes in dojo/finding/ui/filters.py, via get_finding_filterset_fields() in dojo/filters.py). Those are intentionally out of scope here — Meta.fields is consumed by django_filters at class-definition time, so making them import-safe is a larger refactor that deserves its own PR and filter tests.

ImportScanForm and ReImportScanForm declared their finding-group fields
(group_by, create_finding_groups_for_all_findings) in the class body
guarded by is_finding_groups_enabled(). That helper reads a DB-backed
system setting, so merely importing dojo.forms issued a SELECT. When the
module is imported during app initialization (e.g. from an AppConfig
ready() hook), Django raises:

  RuntimeWarning: Accessing the database during app initialization is
  discouraged.

Move the conditional field construction into each form's __init__, so
importing the module never touches the database. Behavior is unchanged:
instances still gain the fields when finding groups are enabled, the
fields keep their original position (appended last), and the empty
default choice is still inserted. Evaluating the setting per-instance
instead of once at import is also more correct if the setting changes.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@Maffooch Maffooch requested a review from mtesauro as a code owner July 13, 2026 18:43
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.

1 participant