Skip to content

Commit 52a7bc1

Browse files
author
Ryan P Kilby
committed
Warn about ordering field use
1 parent 9b40a58 commit 52a7bc1

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

README.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,13 @@ Yes you can. `django-rest-framework-filters` extends `django-filter`, and you c
280280
``django-rest-framework-filters``, but you can use ``RelatedFilter`` to
281281
link to a filter relation defined elsewhere that uses vanilla ``django-filter``.
282282
283+
Caveats
284+
~~~~~~~
285+
286+
djangorestframework-filters is not compatible with the filterset's `order_by`
287+
meta option. Use `rest_framework.filters.OrderingFilter` instead if you need
288+
to support ordering.
289+
283290
284291
License
285292
-------

rest_framework_filters/filterset.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ def __new__(cls, name, bases, attrs):
3939
new_class = super(FilterSetMetaclass, cls).__new__(cls, name, bases, attrs)
4040
fix_filter_field = _get_fix_filter_field(new_class)
4141

42+
# order_by is not compatible.
43+
if new_class._meta.order_by:
44+
new_class._meta.order_by = False
45+
warnings.warn(
46+
'order_by is no longer supported. Use '
47+
'rest_framework.filters.OrderingFilter instead. See: '
48+
'https://github.com/philipn/django-rest-framework-filters/issues/62',
49+
DeprecationWarning, stacklevel=2
50+
)
51+
4252
# Populate our FilterSet fields with all the possible
4353
# filters for the AllLookupsFilter field.
4454
for name, filter_ in six.iteritems(new_class.base_filters.copy()):

tests/test_deprecations.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ class F(FilterSet):
6161
self.assertEqual(len(w), 0)
6262

6363

64+
class OrderByDeprecationTests(TestCase):
65+
66+
def test_override_notifcation(self):
67+
with warnings.catch_warnings(record=True) as w:
68+
warnings.simplefilter("always")
69+
70+
class F(FilterSet):
71+
class Meta:
72+
model = User
73+
order_by = 'username'
74+
75+
F().qs
76+
self.assertEqual(len(w), 1)
77+
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
78+
79+
6480
class AllLookupsDeprecationTests(TestCase):
6581

6682
def test_ALL_LOOKUPS_notification(self):

0 commit comments

Comments
 (0)