当前位置: 首页>>代码示例>>Python>>正文


Python filters.OrderingFilter方法代码示例

本文整理汇总了Python中rest_framework.filters.OrderingFilter方法的典型用法代码示例。如果您正苦于以下问题:Python filters.OrderingFilter方法的具体用法?Python filters.OrderingFilter怎么用?Python filters.OrderingFilter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在rest_framework.filters的用法示例。


在下文中一共展示了filters.OrderingFilter方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_filter_introspectors

# 需要导入模块: from rest_framework import filters [as 别名]
# 或者: from rest_framework.filters import OrderingFilter [as 别名]
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 
开发者ID:Arello-Mobile,项目名称:py2swagger,代码行数:26,代码来源:filter.py

示例2: test_use_with_ordering_filter

# 需要导入模块: from rest_framework import filters [as 别名]
# 或者: from rest_framework.filters import OrderingFilter [as 别名]
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',)) 
开发者ID:percipient,项目名称:django-querysetsequence,代码行数:19,代码来源:test_pagination.py

示例3: test_fallback_gracefully

# 需要导入模块: from rest_framework import filters [as 别名]
# 或者: from rest_framework.filters import OrderingFilter [as 别名]
def test_fallback_gracefully(self):
        # Note: can't use override settings because of how restframework handle settings :(
        #from django_elasticsearch.tests.urls import TestViewSet
        from rest_framework.filters import DjangoFilterBackend, OrderingFilter
        from rest_framework.settings import api_settings

        api_settings.DEFAULT_FILTER_BACKENDS = (DjangoFilterBackend, OrderingFilter)
        # TODO: better way to fake es cluster's death ?

        with mock.patch.object(es_client, 'search') as mock_search:
            mock_search.side_effect = TransportError()
            with mock.patch.object(es_client, 'count') as mock_count:
                mock_count.side_effect = TransportError()
                with mock.patch.object(es_client, 'get') as mock_get:
                    mock_get.side_effect = TransportError()
                    # should fallback to a regular django queryset / filtering
                    r = self.client.get('/rf/tests/')
                    self.assertEqual(r.status_code, 200)
                    self.assertEqual(r.data['filter_status'], 'Failed')
                    self.assertEqual(r.data['count'], 3)
                    self._test_filter_backend_filters()
                    self._test_pagination() 
开发者ID:liberation,项目名称:django-elasticsearch,代码行数:24,代码来源:test_restframework.py

示例4: test_parameters

# 需要导入模块: from rest_framework import filters [as 别名]
# 或者: from rest_framework.filters import OrderingFilter [as 别名]
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) 
开发者ID:Arello-Mobile,项目名称:py2swagger,代码行数:14,代码来源:test_filter_introspector.py

示例5: remove_invalid_fields

# 需要导入模块: from rest_framework import filters [as 别名]
# 或者: from rest_framework.filters import OrderingFilter [as 别名]
def remove_invalid_fields(self, queryset, fields, view, request):
        """
        Extend :py:meth:`rest_framework.filters.OrderingFilter.remove_invalid_fields` to
        validate that all provided sort fields exist (as contrasted with the super's behavior
        which is to silently remove invalid fields).

        :raises ValidationError: if a sort field is invalid.
        """
        valid_fields = [
            item[0] for item in self.get_valid_fields(queryset, view,
                                                      {'request': request})
        ]
        bad_terms = [
            term for term in fields
            if format_value(term.replace(".", "__").lstrip('-'), "underscore") not in valid_fields
        ]
        if bad_terms:
            raise ValidationError('invalid sort parameter{}: {}'.format(
                ('s' if len(bad_terms) > 1 else ''), ','.join(bad_terms)))
        # this looks like it duplicates code above, but we want the ValidationError to report
        # the actual parameter supplied while we want the fields passed to the super() to
        # be correctly rewritten.
        # The leading `-` has to be stripped to prevent format_value from turning it into `_`.
        underscore_fields = []
        for item in fields:
            item_rewritten = item.replace(".", "__")
            if item_rewritten.startswith('-'):
                underscore_fields.append(
                    '-' + format_value(item_rewritten.lstrip('-'), "underscore"))
            else:
                underscore_fields.append(format_value(item_rewritten, "underscore"))

        return super(OrderingFilter, self).remove_invalid_fields(
            queryset, underscore_fields, view, request) 
开发者ID:django-json-api,项目名称:django-rest-framework-json-api,代码行数:36,代码来源:filters.py

示例6: test_adding_filter_backends

# 需要导入模块: from rest_framework import filters [as 别名]
# 或者: from rest_framework.filters import OrderingFilter [as 别名]
def test_adding_filter_backends(self):
        self.assertIn(filters.OrderingFilter, SampleApiV1.filter_backends)
        self.assertIn(DjangoFilterBackend, SampleApiV1.filter_backends) 
开发者ID:HealthByRo,项目名称:drf_tweaks,代码行数:5,代码来源:test_autofilter.py

示例7: test_adding_filter_backends_with_existing

# 需要导入模块: from rest_framework import filters [as 别名]
# 或者: from rest_framework.filters import OrderingFilter [as 别名]
def test_adding_filter_backends_with_existing(self):
        self.assertIn(filters.OrderingFilter, SampleApiV2.filter_backends)
        self.assertIn(DjangoFilterBackend, SampleApiV2.filter_backends) 
开发者ID:HealthByRo,项目名称:drf_tweaks,代码行数:5,代码来源:test_autofilter.py


注:本文中的rest_framework.filters.OrderingFilter方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。