Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
b4b2dc18
Commit
b4b2dc18
authored
Aug 20, 2015
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clean-up refactoring of SearchFilter implementation
parent
aa4cd7e9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
18 deletions
+28
-18
rest_framework/compat.py
+7
-0
rest_framework/filters.py
+21
-18
No files found.
rest_framework/compat.py
View file @
b4b2dc18
...
...
@@ -50,6 +50,13 @@ def total_seconds(timedelta):
return
(
timedelta
.
days
*
86400.0
)
+
float
(
timedelta
.
seconds
)
+
(
timedelta
.
microseconds
/
1000000.0
)
def
distinct
(
queryset
,
base
):
if
settings
.
DATABASES
[
queryset
.
db
][
"ENGINE"
]
==
"django.db.backends.oracle"
:
# distinct analogue for Oracle users
return
base
.
filter
(
pk__in
=
set
(
queryset
.
values_list
(
'pk'
,
flat
=
True
)))
return
queryset
.
distinct
()
# OrderedDict only available in Python 2.7.
# This will always be the case in Django 1.7 and above, as these versions
# no longer support Python 2.6.
...
...
rest_framework/filters.py
View file @
b4b2dc18
...
...
@@ -7,12 +7,13 @@ from __future__ import unicode_literals
import
operator
from
functools
import
reduce
from
django.conf
import
settings
from
django.core.exceptions
import
ImproperlyConfigured
from
django.db
import
models
from
django.utils
import
six
from
rest_framework.compat
import
django_filters
,
get_model_name
,
guardian
from
rest_framework.compat
import
(
distinct
,
django_filters
,
get_model_name
,
guardian
)
from
rest_framework.settings
import
api_settings
FilterSet
=
django_filters
and
django_filters
.
FilterSet
or
None
...
...
@@ -99,25 +100,27 @@ class SearchFilter(BaseFilterBackend):
def
filter_queryset
(
self
,
request
,
queryset
,
view
):
search_fields
=
getattr
(
view
,
'search_fields'
,
None
)
if
not
search_fields
:
return
queryset
original_queryset
=
queryset
orm_lookups
=
[
self
.
construct_search
(
six
.
text_type
(
search_field
))
for
search_field
in
search_fields
]
orm_lookups
=
[
self
.
construct_search
(
six
.
text_type
(
search_field
))
for
search_field
in
search_fields
]
search_terms
=
self
.
get_search_terms
(
request
)
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
))
if
not
search_fields
or
not
search_terms
:
return
queryset
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
)))
else
:
queryset
=
queryset
.
distinct
()
base
=
queryset
for
search_term
in
search_terms
:
queries
=
[
models
.
Q
(
**
{
orm_lookup
:
search_term
})
for
orm_lookup
in
orm_lookups
]
queryset
=
queryset
.
filter
(
reduce
(
operator
.
or_
,
queries
))
return
queryset
# Filtering against a many-to-many field requires us to
# call queryset.distinct() in order to avoid duplicate items
# in the resulting queryset.
return
distinct
(
queryset
,
base
)
class
OrderingFilter
(
BaseFilterBackend
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment