Skip to content

Commit d8bdda9

Browse files
author
Jay Hale
committed
Add back support for django-filter < 2
1 parent 9152075 commit d8bdda9

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ matrix:
4545
env: TEST_TYPE=build DJANGO_VERSION=2.1
4646
- python: '3.6'
4747
env: TEST_TYPE=build DJANGO_VERSION=2.1
48+
- python: '2.7'
49+
env: TEST_TYPE=lint
4850
- python: '3.6'
4951
env: TEST_TYPE=lint
5052
deploy:

graphene_django/filter/filterset.py

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

33
from django.db import models
4-
from django_filters import Filter, MultipleChoiceFilter
4+
from django_filters import Filter, MultipleChoiceFilter, VERSION
55
from django_filters.filterset import BaseFilterSet, FilterSet
66
from django_filters.filterset import FILTER_FOR_DBFIELD_DEFAULTS
77

@@ -51,6 +51,37 @@ class GrapheneFilterSetMixin(BaseFilterSet):
5151
)
5252

5353

54+
# To support a Django 1.11 + Python 2.7 combination django-filter must be
55+
# < 2.x.x. To support the earlier version of django-filter, the
56+
# filter_for_reverse_field method must be present on GrapheneFilterSetMixin and
57+
# must not be present for later versions of django-filter.
58+
if VERSION[0] < 2:
59+
from django.utils.text import capfirst
60+
61+
class GrapheneFilterSetMixinPython2(GrapheneFilterSetMixin):
62+
63+
@classmethod
64+
def filter_for_reverse_field(cls, f, name):
65+
"""Handles retrieving filters for reverse relationships
66+
We override the default implementation so that we can handle
67+
Global IDs (the default implementation expects database
68+
primary keys)
69+
"""
70+
try:
71+
rel = f.field.remote_field
72+
except AttributeError:
73+
rel = f.field.rel
74+
default = {"name": name, "label": capfirst(rel.related_name)}
75+
if rel.multiple:
76+
# For to-many relationships
77+
return GlobalIDMultipleChoiceFilter(**default)
78+
else:
79+
# For to-one relationships
80+
return GlobalIDFilter(**default)
81+
82+
GrapheneFilterSetMixin = GrapheneFilterSetMixinPython2
83+
84+
5485
def setup_filterset(filterset_class):
5586
""" Wrap a provided filterset in Graphene-specific functionality
5687
"""

0 commit comments

Comments
 (0)