How to show query parameter options in Django REST Framework - Swagger
Okay, for those who stumble upon this question, I have figured it out. It is rather silly, and I feel a little stupid for not knowing, but in my defense, it was not clearly documented. The information was not found in DRF documentation, or inside Django REST Swagger repository. Instead it was found under django-rest-framework-docs, which is what Django REST Swagger is built off of.
To specify your query parameter to show up in your SwaggerUI as a form field, you simply comment like so:
def list(self):
"""
param1 -- A first parameter
param2 -- A second parameter
"""
...
And swagger will parse your comments and will put a form input for param1 and param2. What follows --
are the descriptions for the parameters.
New swagger
from rest_framework.filters import BaseFilterBackend
import coreapi
class SimpleFilterBackend(BaseFilterBackend):
def get_schema_fields(self, view):
return [coreapi.Field(
name='query',
location='query',
required=False,
type='string'
)]
class MyViewSet(viewsets.ViewSet):
filter_backends = (SimpleFilterBackend,)
def list(self, request, *args, **kwargs):
# print(request.GET.get('query')) # Use the query param in your view
return Response({'hello': 'world'}, status.HTTP_200_OK)