我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用rest_framework.filters.SearchFilter()。
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 ))
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])