Python rest_framework.filters 模块,OrderingFilter() 实例源码

我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用rest_framework.filters.OrderingFilter()

项目:django-querysetsequence    作者:percipient    | 项目源码 | 文件源码
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',))
项目:py2swagger    作者:Arello-Mobile    | 项目源码 | 文件源码
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
项目:drf_tweaks    作者:ArabellaTech    | 项目源码 | 文件源码
def test_adding_filter_backends(self):
        self.assertIn(filters.OrderingFilter, SampleApiV1.filter_backends)
        self.assertIn(DjangoFilterBackend, SampleApiV1.filter_backends)
项目:drf_tweaks    作者:ArabellaTech    | 项目源码 | 文件源码
def test_adding_filter_backends_with_existing(self):
        self.assertIn(filters.OrderingFilter, SampleApiV2.filter_backends)
        self.assertIn(DjangoFilterBackend, SampleApiV2.filter_backends)
项目:py2swagger    作者:Arello-Mobile    | 项目源码 | 文件源码
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)