@@ -1459,6 +1459,7 @@ class Meta:
1459
1459
f = F ({"favorite_books__title" : "Rainbow Six" }, queryset = qs )
1460
1460
self .assertQuerySetEqual (f .qs , ["alex" ], lambda o : o .username )
1461
1461
1462
+ # No choices given, so filtering does nothing.
1462
1463
class F (FilterSet ):
1463
1464
favorite_books__title = MultipleChoiceFilter ()
1464
1465
@@ -1468,10 +1469,25 @@ class Meta:
1468
1469
1469
1470
f = F ()
1470
1471
self .assertEqual (len (f .filters ["favorite_books__title" ].field .choices ), 0 )
1471
- # f = F({'favorite_books__title': ['1', '3']},
1472
- # queryset=qs)
1473
- # self.assertQuerySetEqual(
1474
- # f.qs, ['aaron', 'alex'], lambda o: o.username)
1472
+
1473
+ # Specifying choices allows filter to work. (See also AllValues variants.)
1474
+ class F (FilterSet ):
1475
+ favorite_books__title = MultipleChoiceFilter (
1476
+ choices = [
1477
+ (b .title , b .title ) for b in Book .objects .all ()
1478
+ ]
1479
+ )
1480
+
1481
+ class Meta :
1482
+ model = User
1483
+ fields = ["favorite_books__title" ]
1484
+
1485
+ f = F ({'favorite_books__title' : ["Ender's Game" , "Snowcrash" ]}, queryset = qs )
1486
+ self .assertIs (True , f .form .is_valid (), list (f .filters ["favorite_books__title" ].field .choices ))
1487
+ self .assertQuerySetEqual (
1488
+ f .qs , ['aaron' , 'alex' ],
1489
+ lambda o : o .username ,
1490
+ )
1475
1491
1476
1492
class F (FilterSet ):
1477
1493
favorite_books__title = AllValuesFilter ()
0 commit comments