I'm using Django REST Framework with django-rest-swagger to create browsable interface for my API. I can specify a request body serializer using YAML docstring , but I haven't found a way to specify a serializer for request query parameters. The view I am using is pretty like:
class ListBans(BaseBanView):
def get(self, request):
"""
List all profile bans
---
response_serializer: backend_serializers.BanSerializer
request_serializer: moderator_serializers.ListBansSerializer
"""
serializer = moderator_serializers.ListBansSerializer(data=request.query_params)
if serializer.is_valid(raise_exception=True):
# query profile bans
data = []
return APIResponse(status=status.HTTP_200_OK, data=data)
class ListBansSerializer(serializers.Serializer):
limit = serializers.IntegerField(default=10, help_text='query limit')
offset = serializers.IntegerField(default=0, help_text='query offset')
what I am trying to achieve is to make django-rest-swagger
create form fields for query parameters from ListBansSerializer
so that I wouldn't have to specify the parameters
section manually in the docstring. Is there a way to do that?
I've created a simple decorator that automatically appends query parameters to the docstring. The source is available at github . Here's a usage example:
class ListBansSerializer(serializers.Serializer):
limit = serializers.IntegerField(default=10, help_text='query limit')
offset = serializers.IntegerField(default=0, help_text='query offset')
class ListBans(BaseBanView):
@add_query_parameters(ListBansSerializer)
def get(self, request):
"""
List all profile bans
---
response_serializer: backend_serializers.BanSerializer
"""
serializer = moderator_serializers.ListBansSerializer(data=request.query_params)
if serializer.is_valid(raise_exception=True):
# query profile bans
data = []
return APIResponse(status=status.HTTP_200_OK, data=data)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.