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


Python rest_framework.DjangoFilterBackend方法代碼示例

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


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

示例1: test_viewset_factory

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

示例2: get_filterset_kwargs

# 需要導入模塊: from django_filters import rest_framework [as 別名]
# 或者: from django_filters.rest_framework import DjangoFilterBackend [as 別名]
def get_filterset_kwargs(self, request, queryset, view):
        """
        Turns filter[<field>]=<value> into <field>=<value> which is what
        DjangoFilterBackend expects

        :raises ValidationError: for bad filter syntax
        """
        filter_keys = []
        # rewrite filter[field] query params to make DjangoFilterBackend work.
        data = request.query_params.copy()
        for qp, val in request.query_params.lists():
            m = self.filter_regex.match(qp)
            if m and (not m.groupdict()['assoc'] or
                      m.groupdict()['ldelim'] != '[' or m.groupdict()['rdelim'] != ']'):
                raise ValidationError("invalid query parameter: {}".format(qp))
            if m and qp != self.search_param:
                if not all(val):
                    raise ValidationError("missing value for query parameter {}".format(qp))
                # convert jsonapi relationship path to Django ORM's __ notation
                key = m.groupdict()['assoc'].replace('.', '__')
                # undo JSON_API_FORMAT_FIELD_NAMES conversion:
                key = format_value(key, 'underscore')
                data.setlist(key, val)
                filter_keys.append(key)
                del data[qp]
        return {
            'data': data,
            'queryset': queryset,
            'request': request,
            'filter_keys': filter_keys,
        } 
開發者ID:django-json-api,項目名稱:django-rest-framework-json-api,代碼行數:33,代碼來源:backends.py

示例3: test_adding_filter_backends

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

示例4: test_adding_filter_backends_with_existing

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

示例5: test_inheritance

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


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