mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 08:14:16 +03:00
Fix SearchFilter.must_call_distinict for annotation+m2m (#7146)
* Test SearchFilter annotation+m2m distinct * Fix SearchFilter annotation+m2m distinct
This commit is contained in:
parent
4ac0fae75b
commit
8a38991d6a
|
@ -85,7 +85,7 @@ class SearchFilter(BaseFilterBackend):
|
||||||
search_field = search_field[1:]
|
search_field = search_field[1:]
|
||||||
# Annotated fields do not need to be distinct
|
# Annotated fields do not need to be distinct
|
||||||
if isinstance(queryset, models.QuerySet) and search_field in queryset.query.annotations:
|
if isinstance(queryset, models.QuerySet) and search_field in queryset.query.annotations:
|
||||||
return False
|
continue
|
||||||
parts = search_field.split(LOOKUP_SEP)
|
parts = search_field.split(LOOKUP_SEP)
|
||||||
for part in parts:
|
for part in parts:
|
||||||
field = opts.get_field(part)
|
field = opts.get_field(part)
|
||||||
|
|
|
@ -406,6 +406,21 @@ class SearchFilterAnnotatedFieldTests(TestCase):
|
||||||
assert len(response.data) == 1
|
assert len(response.data) == 1
|
||||||
assert response.data[0]['title_text'] == 'ABCDEF'
|
assert response.data[0]['title_text'] == 'ABCDEF'
|
||||||
|
|
||||||
|
def test_must_call_distinct_subsequent_m2m_fields(self):
|
||||||
|
f = filters.SearchFilter()
|
||||||
|
|
||||||
|
queryset = SearchFilterModelM2M.objects.annotate(
|
||||||
|
title_text=Upper(
|
||||||
|
Concat(models.F('title'), models.F('text'))
|
||||||
|
)
|
||||||
|
).all()
|
||||||
|
|
||||||
|
# Sanity check that m2m must call distinct
|
||||||
|
assert f.must_call_distinct(queryset, ['attributes'])
|
||||||
|
|
||||||
|
# Annotated field should not prevent m2m must call distinct
|
||||||
|
assert f.must_call_distinct(queryset, ['title_text', 'attributes'])
|
||||||
|
|
||||||
|
|
||||||
class OrderingFilterModel(models.Model):
|
class OrderingFilterModel(models.Model):
|
||||||
title = models.CharField(max_length=20, verbose_name='verbose title')
|
title = models.CharField(max_length=20, verbose_name='verbose title')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user