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

Added MultipleChoiceFilter example test. #1604

Merged
merged 1 commit into from
Aug 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 20 additions & 4 deletions tests/test_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,7 @@ class Meta:
f = F({"favorite_books__title": "Rainbow Six"}, queryset=qs)
self.assertQuerySetEqual(f.qs, ["alex"], lambda o: o.username)

# No choices given, so filtering does nothing.
class F(FilterSet):
favorite_books__title = MultipleChoiceFilter()

Expand All @@ -1468,10 +1469,25 @@ class Meta:

f = F()
self.assertEqual(len(f.filters["favorite_books__title"].field.choices), 0)
# f = F({'favorite_books__title': ['1', '3']},
# queryset=qs)
# self.assertQuerySetEqual(
# f.qs, ['aaron', 'alex'], lambda o: o.username)

# Specifying choices allows filter to work. (See also AllValues variants.)
class F(FilterSet):
favorite_books__title = MultipleChoiceFilter(
choices=[
(b.title, b.title) for b in Book.objects.all()
]
)

class Meta:
model = User
fields = ["favorite_books__title"]

f = F({'favorite_books__title': ["Ender's Game", "Snowcrash"]}, queryset=qs)
self.assertIs(True, f.form.is_valid(), list(f.filters["favorite_books__title"].field.choices))
self.assertQuerySetEqual(
f.qs, ['aaron', 'alex'],
lambda o: o.username,
)

class F(FilterSet):
favorite_books__title = AllValuesFilter()
Expand Down