ClassifierSelectWidget should respect `limit_choices_to` on model field
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
Research direction
Start at ClassifierSelectWidget.optgroups and inspect how its classifier and term iteration uses self.choices.queryset. Reproduce the provided ParentalManyToManyField example, then verify that only matching terms and non-empty classifier groups are shown and invalid selections no longer pass to form validation.
Written by the indexing model from the issue text.
Description
Describe the bug
ClassifierSelectWidget always shows all classifiers and terms even if the underlying model field definition restricts choices using limit_choices_to. This allows users to click on invalid options causing the form to fail validation.
Steps to reproduce
Steps to reproduce the behavior:
- Set up a classifier with multiple terms. For this example I'm imagining a classifier called "Genre" with the terms "Action", "Adventure", "Horror", "Thriller", "Science-Fiction".
- Make a snippet model (new or existing) with the following field:
classifier_terms = ParentalManyToManyField(
'coderedcms.ClassifierTerm',
blank=True,
related_name="genres",
limit_choices_to=Q(name__startswith="A"), # or any filter eliminates some terms.
)
- In the model's
panelsattribute, includeFieldPanel("classifier_terms", widget=ClassifierSelectWidget()). - Run migrations, register snippet, etc.
- Go to the snippet edit page for the snippet.
- Scroll to the "classifier terms" panel. See that all of the terms are present, not just ones matching the
Qobject. - Select both a matching term and a non-matching term, and save the form. The form validation should fail and the error message should complain only about the ID of the non-matching term.
Expected behavior
Only the terms and classifiers matching the limit_choices_to filter should be available as choices, as happens when not using the ClassifierSelectWidget(). CheckboxSelectMultiple is very close to what I want, but it is not grouped by classifier.
Additional context
This should theoretically be a simple matter of updating the optgroups class to use self.choices.queryset.
I made a simple working example by changing the initial classifier queryset from Classifier.objects.all().select_related() to
classifiers = Classifier.objects.all().select_related().prefetch_related(
Prefetch("terms", queryset=self.choices.queryset)
)
to stop Classifiers with no matching terms from appearing as empty groupings, I just changed the inner loop to this:
if terms := classifier.terms.all():
for term in terms:
If this seems like something y'all would be interested in implementing, I could try my hand at making a merge request. Wanted to confirm that the team would consider it worthwhile.
- Dominant language
- Python
- Stars
- 765
- Forks
- 154
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from coderedcorp/coderedcms
-
Type: Bug
Difficulty 1/5 Under an hour Newbie friendliness 78/100
coderedcorp/coderedcms#714 ·
-
Type: Bug
Difficulty 1/5 Under an hour Newbie friendliness 65/100
coderedcorp/coderedcms#711 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
coderedcorp/coderedcms#710 · 4 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 58/100
coderedcorp/coderedcms#709 ·
-
Type: Bug
Difficulty 2/5 1-3 hours Newbie friendliness 55/100
coderedcorp/coderedcms#707 · 1 comment ·
All issues in coderedcorp/coderedcms
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
syfoud/Simulated_Scepter#172 ·
-
A cancelled tests run makes the coverage comment workflow fail and reports it as a red check on main Openarea: ci bug perceived difficulty: 3
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Nitjsefnie-Harness-Commons/daedalus#921 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
EleutherAI/lm-evaluation-harness#4207 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
ClickHouse/clickhouse-connect#1057 ·