Python django_filters.rest_framework.DjangoFilterBackend() Examples
The following are 5
code examples of django_filters.rest_framework.DjangoFilterBackend().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django_filters.rest_framework
, or try the search function
.
Example #1
Source File: test_unit.py From drf-schema-adapter with MIT License | 6 votes |
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 ))
Example #2
Source File: backends.py From django-rest-framework-json-api with BSD 2-Clause "Simplified" License | 5 votes |
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, }
Example #3
Source File: test_autofilter.py From drf_tweaks with MIT License | 5 votes |
def test_adding_filter_backends(self): self.assertIn(filters.OrderingFilter, SampleApiV1.filter_backends) self.assertIn(DjangoFilterBackend, SampleApiV1.filter_backends)
Example #4
Source File: test_autofilter.py From drf_tweaks with MIT License | 5 votes |
def test_adding_filter_backends_with_existing(self): self.assertIn(filters.OrderingFilter, SampleApiV2.filter_backends) self.assertIn(DjangoFilterBackend, SampleApiV2.filter_backends)
Example #5
Source File: test_unit.py From drf-schema-adapter with MIT License | 5 votes |
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])