diff --git a/rest_framework/schemas/inspectors.py b/rest_framework/schemas/inspectors.py index bdd720938b..30f639f5ba 100644 --- a/rest_framework/schemas/inspectors.py +++ b/rest_framework/schemas/inspectors.py @@ -337,18 +337,33 @@ def get_pagination_fields(self, path, method): paginator = view.pagination_class() return paginator.get_schema_fields(view) - def get_filter_fields(self, path, method): - view = self.view + def _allows_filters(self, path, method): + """ + Determine whether to include filter Fields in schema. - if not is_list_view(path, method, view): - return [] + Default implementation looks for ModelViewSet or GenericAPIView + actions/methods that cause filtering on the default implementation. + + Override to adjust behaviour for your view. - if not getattr(view, 'filter_backends', None): + Note: Introduced in v3.7: Initially "private" (i.e. with leading underscore) + to allow changes based on user experience. + """ + if getattr(self.view, 'filter_backends', None) is None: + return False + + if hasattr(self.view, 'action'): + return self.view.action in ["list", "retrieve", "update", "partial_update", "destroy"] + + return method.lower in ["get", "put", "patch", "delete"] + + def get_filter_fields(self, path, method): + if not self._allows_filters(path, method): return [] fields = [] - for filter_backend in view.filter_backends: - fields += filter_backend().get_schema_fields(view) + for filter_backend in self.view.filter_backends: + fields += filter_backend().get_schema_fields(self.view) return fields def get_encoding(self, path, method): diff --git a/tests/test_schemas.py b/tests/test_schemas.py index 184401a868..07c49b71d1 100644 --- a/tests/test_schemas.py +++ b/tests/test_schemas.py @@ -139,7 +139,8 @@ def test_anonymous_request(self): url='/example/{id}/', action='get', fields=[ - coreapi.Field('id', required=True, location='path', schema=coreschema.String()) + coreapi.Field('id', required=True, location='path', schema=coreschema.String()), + coreapi.Field('ordering', required=False, location='query', schema=coreschema.String(title='Ordering', description='Which field to use when ordering the results.')) ] ) } @@ -179,7 +180,8 @@ def test_authenticated_request(self): url='/example/{id}/', action='get', fields=[ - coreapi.Field('id', required=True, location='path', schema=coreschema.String()) + coreapi.Field('id', required=True, location='path', schema=coreschema.String()), + coreapi.Field('ordering', required=False, location='query', schema=coreschema.String(title='Ordering', description='Which field to use when ordering the results.')) ] ), 'custom_action': coreapi.Link( @@ -225,7 +227,8 @@ def test_authenticated_request(self): fields=[ coreapi.Field('id', required=True, location='path', schema=coreschema.String()), coreapi.Field('a', required=True, location='form', schema=coreschema.String(title='A', description=('A field description'))), - coreapi.Field('b', required=False, location='form', schema=coreschema.String(title='B')) + coreapi.Field('b', required=False, location='form', schema=coreschema.String(title='B')), + coreapi.Field('ordering', required=False, location='query', schema=coreschema.String(title='Ordering', description='Which field to use when ordering the results.')) ] ), 'partial_update': coreapi.Link( @@ -235,14 +238,16 @@ def test_authenticated_request(self): fields=[ coreapi.Field('id', required=True, location='path', schema=coreschema.String()), coreapi.Field('a', required=False, location='form', schema=coreschema.String(title='A', description='A field description')), - coreapi.Field('b', required=False, location='form', schema=coreschema.String(title='B')) + coreapi.Field('b', required=False, location='form', schema=coreschema.String(title='B')), + coreapi.Field('ordering', required=False, location='query', schema=coreschema.String(title='Ordering', description='Which field to use when ordering the results.')) ] ), 'delete': coreapi.Link( url='/example/{id}/', action='delete', fields=[ - coreapi.Field('id', required=True, location='path', schema=coreschema.String()) + coreapi.Field('id', required=True, location='path', schema=coreschema.String()), + coreapi.Field('ordering', required=False, location='query', schema=coreschema.String(title='Ordering', description='Which field to use when ordering the results.')) ] ) } @@ -450,7 +455,8 @@ def test_schema_for_regular_views(self): url='/example1/{id}/', action='get', fields=[ - coreapi.Field('id', required=True, location='path', schema=coreschema.String()) + coreapi.Field('id', required=True, location='path', schema=coreschema.String()), + coreapi.Field('ordering', required=False, location='query', schema=coreschema.String(title='Ordering', description='Which field to use when ordering the results.')) ] ) }
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: