Skip to content

Commit b65181c

Browse files
committed
add compatibility with DRF 3.14
1 parent 58c6288 commit b65181c

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

dynamic_rest/filters.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
from django.db.models import Q, Prefetch, Manager
66
import six
77
from functools import reduce
8+
from rest_framework import __version__ as drf_version
89
from rest_framework import serializers
910
from rest_framework.exceptions import ValidationError
10-
from rest_framework.fields import BooleanField, NullBooleanField
11+
try:
12+
from rest_framework.fields import BooleanField, NullBooleanField
13+
except ImportError:
14+
# DRF >= 3.14.0
15+
from rest_framework.fields import BooleanField
1116
from rest_framework.filters import BaseFilterBackend, OrderingFilter
1217

1318
from dynamic_rest.utils import is_truthy
@@ -26,6 +31,13 @@
2631

2732
patch_prefetch_one_level()
2833

34+
DRF_VERSION = drf_version.split('.')
35+
if int(DRF_VERSION[0]) >= 3 and int(DRF_VERSION[1]) >= 14:
36+
# NullBooleanField deprecated in DRF >= 3.14
37+
DRF_BOOLEAN_FIELD = BooleanField
38+
else:
39+
DRF_BOOLEAN_FIELD = (BooleanField, NullBooleanField)
40+
2941

3042
def OR(a, b):
3143
return a | b
@@ -148,7 +160,7 @@ def rewrite_filters(fs, serializer):
148160
out = {}
149161
for node in fs.values():
150162
filter_key, field = node.generate_query_key(serializer)
151-
if isinstance(field, (BooleanField, NullBooleanField)):
163+
if isinstance(field, DRF_BOOLEAN_FIELD):
152164
node.value = is_truthy(node.value)
153165
out[filter_key] = node.value
154166

0 commit comments

Comments
 (0)