Skip to content

Commit 85780ab

Browse files
Maffoochclaude
andauthored
fix(forms): avoid DB access at import time in Import/ReImport forms (#15233)
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>
1 parent d0a25f3 commit 85780ab

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

dojo/forms.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,18 @@ class ImportScanForm(forms.Form):
324324
initial=False,
325325
)
326326

327-
if is_finding_groups_enabled():
328-
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.")
329-
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)
330-
331327
def __init__(self, *args, **kwargs):
332328
environment = kwargs.pop("environment", None)
333329
endpoints = kwargs.pop("endpoints", None)
334330
api_scan_configuration = kwargs.pop("api_scan_configuration", None)
335331
super().__init__(*args, **kwargs)
332+
# Finding-group fields depend on a DB-backed system setting. Add them here
333+
# rather than in the class body so that importing this module never issues
334+
# a query (which triggers Django's "database access during app
335+
# initialization" warning when the module is imported at startup).
336+
if is_finding_groups_enabled():
337+
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.")
338+
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)
336339
self.fields["active"].initial = self.active_verified_choices[0]
337340
self.fields["verified"].initial = self.active_verified_choices[0]
338341
if environment:
@@ -448,15 +451,18 @@ class ReImportScanForm(forms.Form):
448451
initial=False,
449452
)
450453

451-
if is_finding_groups_enabled():
452-
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")
453-
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)
454-
455454
def __init__(self, *args, test=None, **kwargs):
456455
endpoints = kwargs.pop("endpoints", None)
457456
api_scan_configuration = kwargs.pop("api_scan_configuration", None)
458457
api_scan_configuration_queryset = kwargs.pop("api_scan_configuration_queryset", None)
459458
super().__init__(*args, **kwargs)
459+
# Finding-group fields depend on a DB-backed system setting. Add them here
460+
# rather than in the class body so that importing this module never issues
461+
# a query (which triggers Django's "database access during app
462+
# initialization" warning when the module is imported at startup).
463+
if is_finding_groups_enabled():
464+
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")
465+
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)
460466
self.fields["active"].initial = self.active_verified_choices[0]
461467
self.fields["verified"].initial = self.active_verified_choices[0]
462468
self.scan_type = None

0 commit comments

Comments
 (0)