如何记录基于功能的视图参数?

时间:2021-12-25 03:47:20

I'm developing a REST API with Django 1.11 and Django REST Framework 3.7. I installed Django REST Swagger 2.1 to generate the documentation.

我正在用Django 1.11和Django REST框架3.7开发一个REST API。我安装了Django REST Swagger 2.1以生成文档。

I'm using a function-based view like this:

我使用的是基于功能的视图

from rest_framework.decorators import api_view, permission_classes

@api_view(['POST'])
@permission_classes((permissions.AllowAny,))
def jwt_auth(request, provider, format=None):
    """
    View description here
    """
    pass

如何记录基于功能的视图参数?

As you can see, my view is recognized by Swagger and it has the correct description: "View description here".

正如你所看到的,我的观点被Swagger认出来了,它有一个正确的描述:“这里的观点描述”。

However:

然而:

  • You can see the "Description" column is empty for the provider URL parameter.
  • 您可以看到“Description”列为provider URL参数的空。
  • The POST parameters are not documented (obviously, because there is no way for Swagger to know them)
  • POST参数没有文档记录(显然,因为没有办法让你知道它们)

How can I write documentation for the URL and POST parameters of a function-based view, as well as the responses?

如何为基于功能的视图的URL和POST参数以及响应编写文档?

I tried YAML Docstrings but it seems it's for the old version (0.3.x) and it doesn't work with version 2.x.

我尝试过YAML docstring,但它似乎是针对旧版本(0.3.x)的,而且它与版本2.x不兼容。

2 个解决方案

#1


1  

You can use Schema of DjangoRestFrameWork. http://www.django-rest-framework.org/api-guide/schemas/

您可以使用DjangoRestFrameWork的模式。http://www.django-rest-framework.org/api-guide/schemas/

In your case, you can try the following.

在您的情况下,您可以尝试以下方法。


from rest_framework.decorators import api_view, permission_classes, schema
@api_view(['POST'])
@permission_classes((permissions.AllowAny,))
@schema(custom_schema)
def jwt_auth(request, provider, format=None):
    """
    View description here
    """
    pass

custom schema defintion

自定义模式的定义

import coreapi, coreschema
from rest_framework.schemas import AutoSchema, ManualSchema
custom_schema = AutoSchema(manual_fields=[
    coreapi.Field("username", required=True, location="form", type="string", description="username here"),
    coreapi.Field("password", required=True, location="form", type="string", description="password field"
]

Should do the trick. For more detailed info, visit the link I provided at the top. Basic POST and GET parameters should work in this way.

应该足够了。要了解更多的详细信息,请访问我在顶部提供的链接。基本的POST和GET参数应该以这种方式工作。

#2


0  

Following this github issue, it seems it's not possible for the method-based views as you stated.

根据github的问题,似乎不可能像您所说的那样基于方法的视图。

But I think this link can help you.

但是我认为这个链接可以帮助你。

#1


1  

You can use Schema of DjangoRestFrameWork. http://www.django-rest-framework.org/api-guide/schemas/

您可以使用DjangoRestFrameWork的模式。http://www.django-rest-framework.org/api-guide/schemas/

In your case, you can try the following.

在您的情况下,您可以尝试以下方法。


from rest_framework.decorators import api_view, permission_classes, schema
@api_view(['POST'])
@permission_classes((permissions.AllowAny,))
@schema(custom_schema)
def jwt_auth(request, provider, format=None):
    """
    View description here
    """
    pass

custom schema defintion

自定义模式的定义

import coreapi, coreschema
from rest_framework.schemas import AutoSchema, ManualSchema
custom_schema = AutoSchema(manual_fields=[
    coreapi.Field("username", required=True, location="form", type="string", description="username here"),
    coreapi.Field("password", required=True, location="form", type="string", description="password field"
]

Should do the trick. For more detailed info, visit the link I provided at the top. Basic POST and GET parameters should work in this way.

应该足够了。要了解更多的详细信息,请访问我在顶部提供的链接。基本的POST和GET参数应该以这种方式工作。

#2


0  

Following this github issue, it seems it's not possible for the method-based views as you stated.

根据github的问题,似乎不可能像您所说的那样基于方法的视图。

But I think this link can help you.

但是我认为这个链接可以帮助你。