django-rest-swagger似乎对我没用。我无法记录标题以外的任何内容 [英] django-rest-swagger can't seem to work for me. I can't get it to document anything beyond a title
问题描述
django-rest-swagger似乎放弃了对YAML文档的支持,而用模糊的,未记录在案的处理方式代替了它。我花了最后48个小时来尝试了解如何记录它的post方法参数。
例如:我有这个:
class user_addresses(APIView):
获取所有地址或发布一个新地址
authentication_classes =([JSONWebTokenAuthentication])
def get((自我,请求,格式=无):
地址= Address.objects.filter(owner_id = request) .user.id)
打印(地址)
序列化器= address_serializer(地址,许多=真实)
返回响应(serializer.data)
def post(self ,request,format = None):
serializer = address_serializer(data = request.data)
if serializer.is_valid():
serializer.save()
return Response({ 成功:真,
结果:serializer.validated_data},
status = status.HTTP_201_已创建)
返回响应({'success':False,
'result':serializer.errors},
status = status.HTTP_400_BAD_REQUEST)
但是django-rest-swagger会将其显示为:
有人可以指出我的工作方向吗?可以定义大张旗鼓允许的所有丰富数据,例如发布字段名称(无论是否强制使用)。等等。我只是在这里疯狂地跑来跑去,除了抱怨无能为力之外,什么也找不到。
因此2.0版的想法是使用CoreAPI,即内部 rest框架架构生成,并从中生成
CoreAPI使用序列化器和视图类来完成它的工作。从序列化程序中,它知道需要哪些字段,这些字段是什么类型,并且如果要添加个人描述,可以使用 help_text
参数来完成:
some_field = serializers.Field(help_text ='字段描述')
在您的情况下,问题在于它将无法理解 APIView
与序列化程序之间的关系。我建议采取额外的步骤并转到通用视图或视图集,它们都支持可用于自省的 serializer_class
属性。对于您的示例,这样的事情应该起作用:
#serializer
类AddressSerializer(serializers.ModelSerializer):
line1 = serializers.CharField(help_text ='现场文档!')
类元:
模型=地址
字段='__all__'
read_only_fields ='所有者',
def create(self,validated_data):
validated_data ['owner'] = self.context ['request']。user
return super( ).create(validated_data)
#基于api类的视图
类UserAddresses(generics.ListCreateAPIView):
常规API文档(在摇摇欲坠的视图中不可见)
获得:
特定于GET的文档!
Lorem ipsum
发布:
特定于POST的文档!
Dolor ** sit amet **
authentication_classes =([JSONWebTokenAuthentication])
Permission_classes =权限。经过身份验证的
serializer_class = AddressSerializer
def get_queryset(self):
返回Address.objects.filter(owner_id = self.request.user.id)
对于视图,有一个
It seems like django-rest-swagger dropped support for the YAML documentation, and replaced it with a vague non-documented way to do things. I've spent the last 48 hours trying to understand how I can have it document the parameters that go into my post methods.
For instance: I have this:
class user_addresses(APIView):
"""
get all addresses or post a new one
"""
authentication_classes = ([JSONWebTokenAuthentication])
def get(self, request, format=None):
addresses = Address.objects.filter(owner_id=request.user.id)
print (addresses)
serializer = address_serializer(addresses, many=True)
return Response(serializer.data)
def post(self, request, format=None):
serializer = address_serializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response({'success': True,
'result': serializer.validated_data},
status=status.HTTP_201_CREATED)
return Response({'success': False,
'result': serializer.errors},
status=status.HTTP_400_BAD_REQUEST)
But the django-rest-swagger will show it as:
Can someone point me in the direction of something that works where I can define all the rich data that swagger allows, like the post field names, if they're mandatory or not. etc. i'm just going crazy here running in circle and can't find anything but complaints that there's no way to do this.
So the idea with the 2.0 update was to use CoreAPI, the "internal" rest framework schema generation, and from it generate the swagger spec.
CoreAPI uses serializer and view classes to do its thing. From serializers it knows what fields are required, what type are those fields and if you want to add your personal description you can do so with help_text
parameter:
some_field = serializers.Field(help_text='Field description')
In your case, the problem will be that it won't be able to understand the relationship between the APIView
and your serializer. I suggest to take an extra step and move to generic views or viewsets, all of them support serializer_class
attribute that can be used for the introspection. For your example something like this should work:
# serializer
class AddressSerializer(serializers.ModelSerializer):
line1 = serializers.CharField(help_text='Field documentation!')
class Meta:
model = Address
fields = '__all__'
read_only_fields = 'owner',
def create(self, validated_data):
validated_data['owner'] = self.context['request'].user
return super().create(validated_data)
# api class-based view
class UserAddresses(generics.ListCreateAPIView):
"""
General API documentation (not wisible in the swagger view)
get:
GET-specific documentation!
Lorem ipsum
post:
POST-specific documentation!
Dolor **sit amet**
"""
authentication_classes = ([JSONWebTokenAuthentication])
permission_classes = permissions.IsAuthenticated,
serializer_class = AddressSerializer
def get_queryset(self):
return Address.objects.filter(owner_id=self.request.user.id)
For views there is a specific docstirng format, it's very simple and hopefully, will improve overtime. In any way, you should have a bit more acceptable result now:
这篇关于django-rest-swagger似乎对我没用。我无法记录标题以外的任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!