我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用rest_framework.filters.OrderingFilter()。
def test_use_with_ordering_filter(self): class MockView: filter_backends = (filters.OrderingFilter,) ordering_fields = ['title', 'author'] ordering = 'title' request = Request(factory.get('/', {'ordering': 'author'})) ordering = self.pagination.get_ordering(request, [], MockView()) self.assertEqual(ordering, ('#', 'author',)) request = Request(factory.get('/', {'ordering': '-author'})) ordering = self.pagination.get_ordering(request, [], MockView()) self.assertEqual(ordering, ('#', '-author',)) request = Request(factory.get('/', {'ordering': 'invalid'})) ordering = self.pagination.get_ordering(request, [], MockView()) self.assertEqual(ordering, ('#', 'title',))
def get_filter_introspectors(view): from rest_framework import filters try: from django_filters.rest_framework.backends \ import DjangoFilterBackend as third_party_django_filter_backend except ImportError: third_party_django_filter_backend = filters.DjangoFilterBackend filters_map = { third_party_django_filter_backend: DjangoFilterBackendIntrospector, filters.DjangoFilterBackend: DjangoFilterBackendIntrospector, filters.OrderingFilter: OrderingFilterBackendIntrospector, } filter_backends = getattr(view, 'filter_backends', []) introspectors = [] for backend in filter_backends: backend = backend if inspect.isclass(backend) else backend.__class__ introspectors.append( filters_map.get(backend, BaseFilterBackendIntrospector)(view, backend) ) return introspectors
def test_adding_filter_backends(self): self.assertIn(filters.OrderingFilter, SampleApiV1.filter_backends) self.assertIn(DjangoFilterBackend, SampleApiV1.filter_backends)
def test_adding_filter_backends_with_existing(self): self.assertIn(filters.OrderingFilter, SampleApiV2.filter_backends) self.assertIn(DjangoFilterBackend, SampleApiV2.filter_backends)
def test_parameters(self): expected_result = [OrderedDict({ 'in': 'query', 'name': u'ordering', 'type': 'string', 'enum': ('test_filter_field_1', 'test_filter_field_2'), })] instance = TestOrderingFilterView() introspector = OrderingFilterBackendIntrospector(instance, OrderingFilter) self.assertEqual(expected_result, introspector.parameters)