Skip to content

Commit

Permalink
Added MultipleChoiceFilter example test. (#1604)
Browse files Browse the repository at this point in the history
  • Loading branch information
carltongibson authored Aug 26, 2023
1 parent 5ba87c4 commit 1268b0f
Showing 1 changed file with 20 additions and 4 deletions.
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

0 comments on commit 1268b0f

Please sign in to comment.