Skip to content
Advertisement

How to show query parameter options in Django REST Framework – Swagger

This has been bugging me for a while now.

My ultimate goal is to show query parameter options inside SwaggerUI and give a form input for each query parameter. Similar to how it is displayed when providing a serializer for POST.

I am using a viewset which inherits from GenericViewSet and I have tried the following:

  • provide filter_fields attribute
  • provide and set filter_backends attribute to (filters.DjangoFilterBackend,)
  • provide filter_class defined inside my module.
  • Override options method to provide [actions][GET] information

Here’s a small catch, I am not using any models so I don’t think DjangoFilterBackend will really help me. I am using DjangoRESTFramework to talk to an outside API, and I am simply getting JSON result back, and passing it through to the frontend layer.

Here is a small modified snippet of my code to better explain my problem:

views.py

JavaScript

filters.py

JavaScript

Thank you for looking, and thanks in advance for responding.

Advertisement

Answer

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:

JavaScript

And swagger will parse your comments and will put a form input for param1 and param2. What follows -- are the descriptions for the parameters.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement