We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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.
The text was updated successfully, but these errors were encountered:
Fancy making a PR @saevarom ?
Sorry, something went wrong.
I will probably do that @carltongibson , I'm currently at the Wagtail sprint in Arnhem, Netherlands, where we ran into this issue :)
Great. Have fun! 🥳
Successfully merging a pull request may close this issue.
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:
Solution
Account for the dictionary when accepting choices and flatten it down in the same way as the list of 2-tuples.
The text was updated successfully, but these errors were encountered: