From 8450809caa77e45b3aca9e14fc996cf077032896 Mon Sep 17 00:00:00 2001 From: Cody Maffucci <46459665+Maffooch@users.noreply.github.com> Date: Mon, 13 Jul 2026 12:43:07 -0600 Subject: [PATCH] fix(forms): avoid DB access at import time in Import/ReImport forms 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) --- dojo/forms.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/dojo/forms.py b/dojo/forms.py index c9075e07b6a..c9f747e315c 100644 --- a/dojo/forms.py +++ b/dojo/forms.py @@ -324,15 +324,18 @@ class ImportScanForm(forms.Form): initial=False, ) - if is_finding_groups_enabled(): - group_by = forms.ChoiceField(required=False, choices=Finding_Group.GROUP_BY_OPTIONS, help_text="Choose an option to automatically group new findings by the chosen option.") - create_finding_groups_for_all_findings = forms.BooleanField(help_text="If unchecked, finding groups will only be created when there is more than one grouped finding", required=False, initial=True) - def __init__(self, *args, **kwargs): environment = kwargs.pop("environment", None) endpoints = kwargs.pop("endpoints", None) api_scan_configuration = kwargs.pop("api_scan_configuration", None) super().__init__(*args, **kwargs) + # Finding-group fields depend on a DB-backed system setting. Add them here + # rather than in the class body so that importing this module never issues + # a query (which triggers Django's "database access during app + # initialization" warning when the module is imported at startup). + if is_finding_groups_enabled(): + self.fields["group_by"] = forms.ChoiceField(required=False, choices=Finding_Group.GROUP_BY_OPTIONS, help_text="Choose an option to automatically group new findings by the chosen option.") + self.fields["create_finding_groups_for_all_findings"] = forms.BooleanField(help_text="If unchecked, finding groups will only be created when there is more than one grouped finding", required=False, initial=True) self.fields["active"].initial = self.active_verified_choices[0] self.fields["verified"].initial = self.active_verified_choices[0] if environment: @@ -448,15 +451,18 @@ class ReImportScanForm(forms.Form): initial=False, ) - if is_finding_groups_enabled(): - group_by = forms.ChoiceField(required=False, choices=Finding_Group.GROUP_BY_OPTIONS, help_text="Choose an option to automatically group new findings by the chosen option") - create_finding_groups_for_all_findings = forms.BooleanField(help_text="If unchecked, finding groups will only be created when there is more than one grouped finding", required=False, initial=True) - def __init__(self, *args, test=None, **kwargs): endpoints = kwargs.pop("endpoints", None) api_scan_configuration = kwargs.pop("api_scan_configuration", None) api_scan_configuration_queryset = kwargs.pop("api_scan_configuration_queryset", None) super().__init__(*args, **kwargs) + # Finding-group fields depend on a DB-backed system setting. Add them here + # rather than in the class body so that importing this module never issues + # a query (which triggers Django's "database access during app + # initialization" warning when the module is imported at startup). + if is_finding_groups_enabled(): + self.fields["group_by"] = forms.ChoiceField(required=False, choices=Finding_Group.GROUP_BY_OPTIONS, help_text="Choose an option to automatically group new findings by the chosen option") + self.fields["create_finding_groups_for_all_findings"] = forms.BooleanField(help_text="If unchecked, finding groups will only be created when there is more than one grouped finding", required=False, initial=True) self.fields["active"].initial = self.active_verified_choices[0] self.fields["verified"].initial = self.active_verified_choices[0] self.scan_type = None