當前位置: 首頁>>代碼示例>>Python>>正文


Python filters.DjangoFilterBackend方法代碼示例

本文整理匯總了Python中rest_framework.filters.DjangoFilterBackend方法的典型用法代碼示例。如果您正苦於以下問題:Python filters.DjangoFilterBackend方法的具體用法?Python filters.DjangoFilterBackend怎麽用?Python filters.DjangoFilterBackend使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在rest_framework.filters的用法示例。


在下文中一共展示了filters.DjangoFilterBackend方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: get_filter_introspectors

# 需要導入模塊: from rest_framework import filters [as 別名]
# 或者: from rest_framework.filters import DjangoFilterBackend [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_fallback_gracefully

# 需要導入模塊: from rest_framework import filters [as 別名]
# 或者: from rest_framework.filters import DjangoFilterBackend [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

示例3: test_viewset_factory

# 需要導入模塊: from rest_framework import filters [as 別名]
# 或者: from rest_framework.filters import DjangoFilterBackend [as 別名]
def test_viewset_factory(self):
        viewset = self.endpoint.get_viewset()
        self.assertEqual(viewset.serializer_class, self.endpoint.get_serializer())

        for backend in (DjangoFilterBackend, filters.SearchFilter):
            self.assertNotIn(backend, viewset.filter_backends)

        self.assertEqual(viewset.__name__, 'ProductViewSet')

        viewset = self.alternate_endpoint.get_viewset()

        for attr in ('permission_classes', 'filter_fields', 'search_fields', 'ordering_fields'):
            self.assertEqual(list(getattr(viewset, attr)), list(getattr(self, attr)))

        for backend in ('DjangoFilterBackend', 'SearchFilter', 'OrderingFilter'):
            self.assertIn(backend, [be.__name__ for be in viewset.filter_backends])

        self.assertEqual(viewset.pagination_class.__name__, 'ProductPagination')
        self.assertTrue(issubclass(
            viewset.pagination_class,
            pagination.PageNumberPagination
        )) 
開發者ID:drf-forms,項目名稱:drf-schema-adapter,代碼行數:24,代碼來源:test_unit.py

示例4: test_parameters

# 需要導入模塊: from rest_framework import filters [as 別名]
# 或者: from rest_framework.filters import DjangoFilterBackend [as 別名]
def test_parameters(self):
        expected_result = [
            OrderedDict({
                'in': 'query',
                'name': 'test_filter_field_1',
                'type': 'string',
                'required': False,
                'description': 'Filter parameter',
            }),
            OrderedDict({
                'in': 'query',
                'name': 'test_filter_field_2',
                'type': 'string',
                'required': False,
                'description': 'Filter parameter',
            }),
            OrderedDict({
                'in': 'query',
                'name': 'o',
                'type': 'string',
                'description': 'Ordering parameter',
                'enum': ['test_filter_field_1', 'test_filter_field_2', '-test_filter_field_1', '-test_filter_field_2'],
            }),
        ]

        instance = TestDjangoFilterBackendView()
        introspector = DjangoFilterBackendIntrospector(instance, DjangoFilterBackend)

        self.assertEqual(sorted(expected_result[0]), sorted(introspector.parameters[0]))
        self.assertEqual(sorted(expected_result[1]), sorted(introspector.parameters[1]))
        self.assertEqual(sorted(expected_result[2]), sorted(introspector.parameters[2])) 
開發者ID:Arello-Mobile,項目名稱:py2swagger,代碼行數:33,代碼來源:test_filter_introspector.py

示例5: test_inheritance

# 需要導入模塊: from rest_framework import filters [as 別名]
# 或者: from rest_framework.filters import DjangoFilterBackend [as 別名]
def test_inheritance(self):

        class DummyEndpoint(Endpoint):
            model = Product
            permission_classes = self.permission_classes
            filter_fields = self.filter_fields
            search_fields = self.search_fields
            ordering_fields = self.ordering_fields

        endpoint = DummyEndpoint()

        self.assertEqual(endpoint.model, Product)
        self.assertEqual(len(endpoint.get_fields_for_serializer()), len(self.fields) + 1)
        for field in self.fields:
            self.assertIn(field, endpoint.get_fields_for_serializer())
        self.assertIn('__str__', endpoint.get_fields_for_serializer())

        serializer = endpoint.get_serializer()
        self.assertEqual(serializer.Meta.model, Product)
        self.assertEqual(len(serializer().fields.items()), len(self.endpoint.get_fields_for_serializer()))

        viewset = endpoint.get_viewset()

        for attr in ('permission_classes', 'filter_fields', 'search_fields', 'ordering_fields'):
            self.assertEqual(list(getattr(viewset, attr)), list(getattr(self, attr)))

        for backend in ('DjangoFilterBackend', 'SearchFilter', 'OrderingFilter'):
            self.assertIn(backend, [be.__name__ for be in viewset.filter_backends]) 
開發者ID:drf-forms,項目名稱:drf-schema-adapter,代碼行數:30,代碼來源:test_unit.py


注:本文中的rest_framework.filters.DjangoFilterBackend方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。