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

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

项目:drf-schema-adapter    作者:drf-forms    | 项目源码 | 文件源码
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(getattr(viewset, attr), 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
        ))
项目: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-schema-adapter    作者:drf-forms    | 项目源码 | 文件源码
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.Meta.fields), len(self.endpoint.get_fields_for_serializer()))

        viewset = endpoint.get_viewset()

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

        for backend in ('DjangoFilterBackend', 'SearchFilter', 'OrderingFilter'):
            self.assertIn(backend, [be.__name__ for be in viewset.filter_backends])
项目:py2swagger    作者:Arello-Mobile    | 项目源码 | 文件源码
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]))