From 77612482afa46a8dd3213ee547dfa99cb6ea936c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Clgen=20Sar=C4=B1kavak?= Date: Sun, 2 Nov 2025 14:05:19 +0300 Subject: [PATCH] Remove ordering filter class handling These filters are already applied by GenericAPIView.filter_queryset() --- rest_framework/pagination.py | 20 ++------------------ tests/test_pagination.py | 36 ------------------------------------ 2 files changed, 2 insertions(+), 54 deletions(-) diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index bd55ea2d1..9e68cdfd1 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -615,7 +615,7 @@ class CursorPagination(BasePagination): return None self.base_url = request.build_absolute_uri() - self.ordering = self.get_ordering(request, queryset, view) + self.ordering = self.get_ordering() self.cursor = self.decode_cursor(request) if self.cursor is None: @@ -802,28 +802,12 @@ class CursorPagination(BasePagination): cursor = Cursor(offset=offset, reverse=True, position=position) return self.encode_cursor(cursor) - def get_ordering(self, request, queryset, view): + def get_ordering(self): """ Return a tuple of strings, that may be used in an `order_by` method. """ - # The default case is to check for an `ordering` attribute - # on this pagination instance. ordering = self.ordering - ordering_filters = [ - filter_cls for filter_cls in getattr(view, 'filter_backends', []) - if hasattr(filter_cls, 'get_ordering') - ] - - if ordering_filters: - # If a filter exists on the view that implements `get_ordering` - # then we defer to that filter to determine the ordering. - filter_cls = ordering_filters[0] - filter_instance = filter_cls() - ordering_from_filter = filter_instance.get_ordering(request, queryset, view) - if ordering_from_filter: - ordering = ordering_from_filter - assert ordering is not None, ( 'Using cursor pagination, but no ordering attribute was declared ' 'on the pagination class.' diff --git a/tests/test_pagination.py b/tests/test_pagination.py index 63b897def..af8e82b6b 100644 --- a/tests/test_pagination.py +++ b/tests/test_pagination.py @@ -616,42 +616,6 @@ class CursorPaginationTestsMixin: with pytest.raises(exceptions.NotFound): self.pagination.paginate_queryset(self.queryset, request) - def test_use_with_ordering_filter(self): - class MockView: - filter_backends = (filters.OrderingFilter,) - ordering_fields = ['username', 'created'] - ordering = 'created' - - request = Request(factory.get('/', {'ordering': 'username'})) - ordering = self.pagination.get_ordering(request, [], MockView()) - assert ordering == ('username',) - - request = Request(factory.get('/', {'ordering': '-username'})) - ordering = self.pagination.get_ordering(request, [], MockView()) - assert ordering == ('-username',) - - request = Request(factory.get('/', {'ordering': 'invalid'})) - ordering = self.pagination.get_ordering(request, [], MockView()) - assert ordering == ('created',) - - def test_use_with_ordering_filter_without_ordering_default_value(self): - class MockView: - filter_backends = (filters.OrderingFilter,) - ordering_fields = ['username', 'created'] - - request = Request(factory.get('/')) - ordering = self.pagination.get_ordering(request, [], MockView()) - # it gets the value of `ordering` provided by CursorPagination - assert ordering == ('created',) - - request = Request(factory.get('/', {'ordering': 'username'})) - ordering = self.pagination.get_ordering(request, [], MockView()) - assert ordering == ('username',) - - request = Request(factory.get('/', {'ordering': 'invalid'})) - ordering = self.pagination.get_ordering(request, [], MockView()) - assert ordering == ('created',) - def test_cursor_pagination(self): (previous, current, next, previous_url, next_url) = self.get_pages('/')