Skip to content

Commit dc0c290

Browse files
author
Jay Hale
committed
Making GrapheneFilterSetMixin compatible with django_filter 2
1 parent 14f156e commit dc0c290

File tree

4 files changed

+20
-35
lines changed

4 files changed

+20
-35
lines changed

docs/filtering.rst

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ Filtering
22
=========
33

44
Graphene integrates with
5-
`django-filter <https://django-filter.readthedocs.io/en/1.1.0/>`__ (< 2.0.0) to provide
6-
filtering of results (this also means filtering is only compatible with Django < 2.0).
7-
8-
See the `usage
9-
documentation <https://django-filter.readthedocs.io/en/1.1.0/guide/usage.html#the-filter>`__
5+
`django-filter <https://django-filter.readthedocs.io/en/master/>`__ (2.x for
6+
Python 3 or 1.x for Python 2) to provide filtering of results. See the `usage
7+
documentation <https://django-filter.readthedocs.io/en/master/guide/usage.html#the-filter>`__
108
for details on the format for ``filter_fields``.
119

1210
This filtering is automatically available when implementing a ``relay.Node``.
@@ -17,7 +15,7 @@ You will need to install it manually, which can be done as follows:
1715
.. code:: bash
1816
1917
# You'll need to django-filter
20-
pip install django-filter==1.1.0
18+
pip install django-filter>=2
2119
2220
Note: The techniques below are demoed in the `cookbook example
2321
app <https://github.com/graphql-python/graphene-django/tree/master/examples/cookbook>`__.
@@ -28,7 +26,7 @@ Filterable fields
2826
The ``filter_fields`` parameter is used to specify the fields which can
2927
be filtered upon. The value specified here is passed directly to
3028
``django-filter``, so see the `filtering
31-
documentation <https://django-filter.readthedocs.io/en/1.1.0/guide/usage.html#the-filter>`__
29+
documentation <https://django-filter.readthedocs.io/en/master/guide/usage.html#the-filter>`__
3230
for full details on the range of options available.
3331

3432
For example:
@@ -129,7 +127,7 @@ create your own ``Filterset`` as follows:
129127
all_animals = DjangoFilterConnectionField(AnimalNode,
130128
filterset_class=AnimalFilter)
131129
132-
The context argument is passed on as the `request argument <http://django-filter.readthedocs.io/en/1.1.0/guide/usage.html#request-based-filtering>`__
130+
The context argument is passed on as the `request argument <http://django-filter.readthedocs.io/en/master/guide/usage.html#request-based-filtering>`__
133131
in a ``django_filters.FilterSet`` instance. You can use this to customize your
134132
filters to be context-dependent. We could modify the ``AnimalFilter`` above to
135133
pre-filter animals owned by the authenticated user (set in ``context.user``).

examples/cookbook/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ graphene
22
graphene-django
33
graphql-core>=2.1rc1
44
django==1.9
5-
django-filter<2
5+
django-filter>=2

graphene_django/filter/filterset.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import itertools
22

33
from django.db import models
4-
from django.utils.text import capfirst
54
from django_filters import Filter, MultipleChoiceFilter
65
from django_filters.filterset import BaseFilterSet, FilterSet
76
from django_filters.filterset import FILTER_FOR_DBFIELD_DEFAULTS
@@ -15,7 +14,10 @@ class GlobalIDFilter(Filter):
1514
field_class = GlobalIDFormField
1615

1716
def filter(self, qs, value):
18-
_type, _id = from_global_id(value)
17+
""" Convert the filter value to a primary key before filtering """
18+
_id = None
19+
if value is not None:
20+
_, _id = from_global_id(value)
1921
return super(GlobalIDFilter, self).filter(qs, _id)
2022

2123

@@ -32,37 +34,22 @@ def filter(self, qs, value):
3234
models.OneToOneField: {"filter_class": GlobalIDFilter},
3335
models.ForeignKey: {"filter_class": GlobalIDFilter},
3436
models.ManyToManyField: {"filter_class": GlobalIDMultipleChoiceFilter},
37+
models.ManyToOneRel: {"filter_class": GlobalIDMultipleChoiceFilter},
38+
models.ManyToManyRel: {"filter_class": GlobalIDMultipleChoiceFilter},
3539
}
3640

3741

3842
class GrapheneFilterSetMixin(BaseFilterSet):
43+
""" A django_filters.filterset.BaseFilterSet with default filter overrides
44+
to handle global IDs """
45+
3946
FILTER_DEFAULTS = dict(
4047
itertools.chain(
41-
FILTER_FOR_DBFIELD_DEFAULTS.items(), GRAPHENE_FILTER_SET_OVERRIDES.items()
48+
FILTER_FOR_DBFIELD_DEFAULTS.items(),
49+
GRAPHENE_FILTER_SET_OVERRIDES.items()
4250
)
4351
)
4452

45-
@classmethod
46-
def filter_for_reverse_field(cls, f, name):
47-
"""Handles retrieving filters for reverse relationships
48-
49-
We override the default implementation so that we can handle
50-
Global IDs (the default implementation expects database
51-
primary keys)
52-
"""
53-
try:
54-
rel = f.field.remote_field
55-
except AttributeError:
56-
rel = f.field.rel
57-
58-
default = {"name": name, "label": capfirst(rel.related_name)}
59-
if rel.multiple:
60-
# For to-many relationships
61-
return GlobalIDMultipleChoiceFilter(**default)
62-
else:
63-
# For to-one relationships
64-
return GlobalIDFilter(**default)
65-
6653

6754
def setup_filterset(filterset_class):
6855
""" Wrap a provided filterset in Graphene-specific functionality

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"coveralls",
2020
"mock",
2121
"pytz",
22-
"django-filter<2",
22+
"django-filter>=2",
2323
"pytest-django>=3.3.2",
2424
] + rest_framework_require
2525

@@ -50,7 +50,7 @@
5050
"six>=1.10.0",
5151
"graphene>=2.1,<3",
5252
"graphql-core>=2.1rc1",
53-
"Django>=1.8.0",
53+
"Django>=1.11",
5454
"iso8601",
5555
"singledispatch>=3.4.0.3",
5656
"promise>=2.1",

0 commit comments

Comments
 (0)