summaryrefslogtreecommitdiff
path: root/tests/admin_views/forms.py
diff options
context:
space:
mode:
authorseanhelvey <sean.helvey@gmail.com>2024-12-13 11:56:53 -0800
committerJacob Walls <jacobtylerwalls@gmail.com>2026-01-22 21:12:23 -0500
commitb1ffa9a9d78b0c2c5ad6ed5a1d84e380d5cfd010 (patch)
tree0fcfd9b90c788e21e58cb9249f4119062b8bfc4e /tests/admin_views/forms.py
parent3851601b2e080df34fb9227fe5d2fd43af604263 (diff)
Fixed #13883 -- Rendered named choice groups with <optgroup> in FilteredSelectMultiple.
This patch adds support for <optgroup>s in FilteredSelectMultiple widgets. When a popup returns a new object, if the source field contains optgroup choices, the optgroup is now also included in the response data. Additionally, this adds error handling for invalid source_model parameters to prevent crashes and display user-friendly error messages instead. Co-authored-by: Michael McLarnon <mmclar@gmail.com>
Diffstat (limited to 'tests/admin_views/forms.py')
-rw-r--r--tests/admin_views/forms.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/tests/admin_views/forms.py b/tests/admin_views/forms.py
index 3a3566c10f..f15a5b3ac1 100644
--- a/tests/admin_views/forms.py
+++ b/tests/admin_views/forms.py
@@ -1,7 +1,10 @@
+from django import forms
from django.contrib.admin.forms import AdminAuthenticationForm, AdminPasswordChangeForm
from django.contrib.admin.helpers import ActionForm
from django.core.exceptions import ValidationError
+from .models import Section
+
class CustomAdminAuthenticationForm(AdminAuthenticationForm):
class Media:
@@ -23,3 +26,63 @@ class CustomAdminPasswordChangeForm(AdminPasswordChangeForm):
class MediaActionForm(ActionForm):
class Media:
js = ["path/to/media.js"]
+
+
+class SectionFormWithOptgroups(forms.ModelForm):
+ articles = forms.ChoiceField(
+ choices=[
+ ("Published", [("1", "Test Article")]),
+ ("Draft", [("2", "Other Article")]),
+ ],
+ required=False,
+ )
+
+ class Meta:
+ model = Section
+ fields = ["name", "articles"]
+
+
+class SectionFormWithObjectOptgroups(forms.ModelForm):
+ """Form with model instances as optgroup keys (tests str() conversion)."""
+
+ articles = forms.ChoiceField(required=False)
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ # Use Section instances as optgroup keys
+ sections = Section.objects.all()[:2]
+ if sections:
+ self.fields["articles"].choices = [
+ (sections[0], [("1", "Article 1")]),
+ (
+ sections[1] if len(sections) > 1 else sections[0],
+ [("2", "Article 2")],
+ ),
+ ]
+
+ class Meta:
+ model = Section
+ fields = ["name", "articles"]
+
+
+class SectionFormWithDynamicOptgroups(forms.ModelForm):
+ """
+ Form where the field with optgroups is added dynamically in __init__.
+ This tests that the implementation doesn't rely on accessing the
+ uninstantiated form class's _meta or fields, which wouldn't work here.
+ """
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ # Dynamically add a field with optgroups after instantiation.
+ self.fields["articles"] = forms.ChoiceField(
+ choices=[
+ ("Category A", [("1", "Item 1"), ("2", "Item 2")]),
+ ("Category B", [("3", "Item 3"), ("4", "Item 4")]),
+ ],
+ required=False,
+ )
+
+ class Meta:
+ model = Section
+ fields = ["name"]