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:
我正在使用Django REST Framework和django-rest-swagger为我的API创建可浏览的界面。我可以使用YAML docstring指定请求正文序列化程序,但我还没有找到为请求查询参数指定序列化程序的方法。我使用的视图很像:
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?
我想要实现的是让django-rest-swagger为ListBansSerializer中的查询参数创建表单字段,这样我就不必在docstring中手动指定参数部分。有没有办法做到这一点?
1 个解决方案
#1
4
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:
我创建了一个简单的装饰器,它自动将查询参数附加到docstring。该源可在github上获得。这是一个用法示例:
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)
#1
4
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:
我创建了一个简单的装饰器,它自动将查询参数附加到docstring。该源可在github上获得。这是一个用法示例:
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)