-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix DISTINCT for Oracle databases #2935
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7813d2f
fix DISTINCT for Oracle databases
trollknurr c47ec60
no need to do distinct on every loop cycle & add analogue of distinct…
trollknurr de95598
removed using `view.model.objects`
trollknurr 0906bf2
fix empty and_query
trollknurr 745d8d0
added comment
e8b23c4
thin logic
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -57,6 +58,7 @@ class AutoFilterSet(self.default_filter_set): | |
class Meta: | ||
model = queryset.model | ||
fields = filter_fields | ||
|
||
return AutoFilterSet | ||
|
||
return None | ||
|
@@ -97,14 +99,23 @@ def filter_queryset(self, request, queryset, view): | |
|
||
if not search_fields: | ||
return queryset | ||
|
||
if settings.DATABASES[queryset.db]["ENGINE"] == "django.db.backends.oracle": | ||
# Remember queryset for Oracle db users | ||
original_queryset = queryset | ||
orm_lookups = [self.construct_search(six.text_type(search_field)) | ||
for search_field in search_fields] | ||
|
||
for search_term in self.get_search_terms(request): | ||
or_queries = [models.Q(**{orm_lookup: search_term}) | ||
for orm_lookup in orm_lookups] | ||
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": | ||
# Oracle db don't support distinct on *LOB fields | ||
queryset = queryset.distinct() | ||
|
||
if settings.DATABASES[queryset.db]["ENGINE"] == "django.db.backends.oracle": | ||
# distinct analogue for Oracle users | ||
queryset = original_queryset.filter(pk__in=set(queryset.values_list('pk', flat=True))) | ||
|
||
return queryset | ||
|
||
|
@@ -152,7 +163,7 @@ def remove_invalid_fields(self, queryset, fields, view): | |
field.source or field_name | ||
for field_name, field in serializer_class().fields.items() | ||
if not getattr(field, 'write_only', False) | ||
] | ||
] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't need to be indented another level. This is what is causing Flake8 to fail by the way. |
||
elif valid_fields == '__all__': | ||
# View explicitly allows filtering on any model field | ||
valid_fields = [field.name for field in queryset.model._meta.fields] | ||
|
@@ -174,6 +185,7 @@ class DjangoObjectPermissionsFilter(BaseFilterBackend): | |
A filter backend that limits results to those where the requesting user | ||
has read object level permissions. | ||
""" | ||
|
||
def __init__(self): | ||
assert guardian, 'Using DjangoObjectPermissionsFilter, but django-guardian is not installed' | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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! :)