Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support group mappings as dictionares introduced in Django 5.0 #1667

Closed
saevarom opened this issue Jun 13, 2024 · 3 comments · Fixed by #1668
Closed

Support group mappings as dictionares introduced in Django 5.0 #1667

saevarom opened this issue Jun 13, 2024 · 3 comments · Fixed by #1668

Comments

@saevarom
Copy link
Contributor

Problem
Django 5.0 introduced a new way to define option groups (https://docs.djangoproject.com/en/5.0/releases/5.0/#more-options-for-declaring-field-choices). django-filters already supports option groups via the older method using a list of 2-tuples, but fails to account for the dictionary option. Example using django-filters version 24.2:

>>> import django_filters
>>> django_filters.VERSION
(24, 2)
>>> class MyDateRangeFilter(DateRangeFilter):
...     choices=[ # choices defined using a list of 2-tuples
...         ("Martial Arts", [("judo", "Judo"), ("karate", "Karate")]),
...         ("Racket", [("badminton", "Badminton"), ("tennis", "Tennis")]),
...         ("unknown", "Unknown"),
...     ]
...     filters = {
...         "judo": "Judo",
...         "karate": "Karate",
...         "badminton": "Badminton",
...         "tennis": "Tennis",
...         "unknown": "Unknown",
...     }
...
>>> MyDateRangeFilter()
<MyDateRangeFilter object at 0xffff77bb0eb0>
>>> class MyDateRangeFilter(DateRangeFilter):
...     choices = {  # Using a mapping instead of a list of 2-tuples.
...         "Martial Arts": {"judo": "Judo", "karate": "Karate"},
...         "Racket": {"badminton": "Badminton", "tennis": "Tennis"},
...         "unknown": "Unknown",
...     }
...     filters = {
...         "judo": "Judo",
...         "karate": "Karate",
...         "badminton": "Badminton",
...         "tennis": "Tennis",
...         "unknown": "Unknown",
...     }
...
>>> MyDateRangeFilter()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.8/site-packages/django_filters/filters.py", line 491, in __init__
    assert not unique, (
AssertionError: Keys must be present in both 'choices' and 'filters'. Missing keys: 'M, R, badminton, judo, karate, tennis, u, unknown'

Solution
Account for the dictionary when accepting choices and flatten it down in the same way as the list of 2-tuples.

@carltongibson
Copy link
Owner

Fancy making a PR @saevarom ?

@saevarom
Copy link
Contributor Author

I will probably do that @carltongibson , I'm currently at the Wagtail sprint in Arnhem, Netherlands, where we ran into this issue :)

@carltongibson
Copy link
Owner

Great. Have fun! 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants