You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>
Copy file name to clipboardExpand all lines: dojo/forms.py
+14-8Lines changed: 14 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -324,15 +324,18 @@ class ImportScanForm(forms.Form):
324
324
initial=False,
325
325
)
326
326
327
-
ifis_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)
# 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
+
ifis_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)
@@ -448,15 +451,18 @@ class ReImportScanForm(forms.Form):
448
451
initial=False,
449
452
)
450
453
451
-
ifis_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)
# 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
+
ifis_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)
0 commit comments