Skip to content

Commit

Permalink
django-queryset-sequence support fixes
Browse files Browse the repository at this point in the history
- Fix #1190
- Fix #1187
  • Loading branch information
jpic committed May 3, 2021
1 parent 4a83243 commit 774bddf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/dal/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,18 @@ def get_search_results(self, queryset, search_term):
]
queryset = queryset.filter(reduce(operator.or_, or_queries))

if any(
lookup_needs_distinct(queryset.model._meta, search_spec)
for search_spec in orm_lookups
):
if self.lookup_needs_distinct(queryset, orm_lookups):
queryset = queryset.distinct()

return queryset

def lookup_needs_distinct(self, queryset, orm_lookups):
"""Return True if an orm_lookup requires calling qs.distinct()."""
return any(
lookup_needs_distinct(queryset.model._meta, search_spec)
for search_spec in orm_lookups
)

def create_object(self, text):
"""Create an object given a text."""
return self.get_queryset().get_or_create(
Expand Down
6 changes: 6 additions & 0 deletions src/dal_queryset_sequence/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,9 @@ def get_model_name(self, model):
except IndexError:
pass
return model._meta.verbose_name

def lookup_needs_distinct(self, queryset, orm_lookups):
"""Return True if a orm_lookups requires calling qs.distinct()."""
for qs in queryset._querysets:
if super().lookup_needs_distinct(qs, orm_lookups):
return True
11 changes: 11 additions & 0 deletions test_project/select2_generic_foreign_key/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pytest

from django.urls import reverse

from .forms import TForm


@pytest.mark.django_db
def test_search(client):
url = reverse(TForm.declared_fields['test2'].as_url(TForm).name)
client.get(url + '?q=test+1').json()

0 comments on commit 774bddf

Please sign in to comment.