Skip to content
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion rest_framework/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.core.exceptions import ImproperlyConfigured
from django.db import models
from django.utils import six
from django.conf import settings
from rest_framework.compat import django_filters, guardian, get_model_name
from rest_framework.settings import api_settings
from functools import reduce
Expand Down Expand Up @@ -104,8 +105,13 @@ def filter_queryset(self, request, queryset, view):
for search_term in self.get_search_terms(request):
or_queries = [models.Q(**{orm_lookup: search_term})
for orm_lookup in orm_lookups]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer it if we didn't introduce the and_queries. We should keep each pull request as absolutely minimal as possible. Seems like there's two different changes here - the Oracle support as one, and the change in the filtering style as another. Let's just adopt the first of those two.
Apologies for the slow progress on this, but thanks for getting it nearly there! :)

queryset = queryset.filter(reduce(operator.or_, or_queries)).distinct()
queryset = queryset.filter(reduce(operator.or_, or_queries))

if settings.DATABASES[queryset.db]["ENGINE"] != "django.db.backends.oracle":
queryset = queryset.distinct()
else:
pk_list = queryset.values_list('pk', flat=True)
queryset = view.model.objects.filter(pk__in=set(pk_list))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using view.model.objects here feels a little bit brittle. I think we can avoid needing that. Does something like the following make sense?...

if settings.DATABASES[queryset.db]["ENGINE"] != "django.db.backends.oracle":
    return queryset.filter(reduce(operator.or_, or_queries)).distinct()
else:
    pk_list = queryset.filter(reduce(operator.or_, or_queries)).values_list('pk', flat=True)
    return queryset.filter(pk_in=set(pk_list))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note here: Since when is the model attribute back on views? I thought that was removed.

return queryset


Expand Down