Skip to content

Commit b999db1

Browse files
committed
Merge pull request #2535 from carltongibson/search-filter-duplicates
Fix duplicate results of `ManyToManyField` when using `SearchFilter`.
2 parents 7b639c0 + 3522b69 commit b999db1

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

rest_framework/filters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def filter_queryset(self, request, queryset, view):
104104
for search_term in self.get_search_terms(request):
105105
or_queries = [models.Q(**{orm_lookup: search_term})
106106
for orm_lookup in orm_lookups]
107-
queryset = queryset.filter(reduce(operator.or_, or_queries))
107+
queryset = queryset.filter(reduce(operator.or_, or_queries)).distinct()
108108

109109
return queryset
110110

tests/test_filters.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,56 @@ class SearchListView(generics.ListAPIView):
429429
reload_module(filters)
430430

431431

432+
class AttributeModel(models.Model):
433+
label = models.CharField(max_length=32)
434+
435+
436+
class SearchFilterModelM2M(models.Model):
437+
title = models.CharField(max_length=20)
438+
text = models.CharField(max_length=100)
439+
attributes = models.ManyToManyField(AttributeModel)
440+
441+
442+
class SearchFilterM2MSerializer(serializers.ModelSerializer):
443+
class Meta:
444+
model = SearchFilterModelM2M
445+
446+
447+
class SearchFilterM2MTests(TestCase):
448+
def setUp(self):
449+
# Sequence of title/text/attributes is:
450+
#
451+
# z abc [1, 2, 3]
452+
# zz bcd [1, 2, 3]
453+
# zzz cde [1, 2, 3]
454+
# ...
455+
for idx in range(3):
456+
label = 'w' * (idx + 1)
457+
AttributeModel(label=label)
458+
459+
for idx in range(10):
460+
title = 'z' * (idx + 1)
461+
text = (
462+
chr(idx + ord('a')) +
463+
chr(idx + ord('b')) +
464+
chr(idx + ord('c'))
465+
)
466+
SearchFilterModelM2M(title=title, text=text).save()
467+
SearchFilterModelM2M.objects.get(title='zz').attributes.add(1, 2, 3)
468+
469+
def test_m2m_search(self):
470+
class SearchListView(generics.ListAPIView):
471+
queryset = SearchFilterModelM2M.objects.all()
472+
serializer_class = SearchFilterM2MSerializer
473+
filter_backends = (filters.SearchFilter,)
474+
search_fields = ('=title', 'text', 'attributes__label')
475+
476+
view = SearchListView.as_view()
477+
request = factory.get('/', {'search': 'zz'})
478+
response = view(request)
479+
self.assertEqual(len(response.data), 1)
480+
481+
432482
class OrderingFilterModel(models.Model):
433483
title = models.CharField(max_length=20)
434484
text = models.CharField(max_length=100)

0 commit comments

Comments
 (0)