标签: drf-yasg

django rest framework - 如何将帖子参数添加到api文档(drf_yasg)?

x_param = openapi.Parameter('x', in_=openapi.IN_FORM, description='srring',
                                   type=openapi.TYPE_STRING)

y_param = openapi.Parameter('y', in_=openapi.IN_FORM, description='string',
                                   type=openapi.TYPE_STRING)

@swagger_auto_schema(method='post', manual_parameters=[x_param,y_param])
@api_view(['POST'])
def test(request):
    pass
Run Code Online (Sandbox Code Playgroud)

我用作drf_yasg招摇.

我做了上面的编码并用swagger进行了测试,当我查看Chrome时,请求的有效负载是x = 124 & y = 124124.

并且,通过以下消息,发生了错误的请求错误.

{
  "detail": "JSON parse error - Expecting value: line 1 column 1 (char 0)"
}
Run Code Online (Sandbox Code Playgroud)

将帖子参数添加到swagger是不对的?

django swagger django-rest-framework drf-yasg

11
推荐指数
1
解决办法
1525
查看次数

如何在 ViewSet 文档字符串中明确记录可能的 REST 操作?

DRF文档提到了这一点:

请注意,当使用视图集时,基本文档字符串用于所有生成的视图。要为每个视图提供描述,例如列表和检索视图,请使用文档字符串部分,如架构作为文档:示例中所述。

但是链接不好,类似的链接https://www.django-rest-framework.org/api-guide/schemas/没有提到这些“部分”。

我如何在我的单个 Viewset 中清楚地记录我不同的可能的 REST 操作,当它组成时,

class ViewSet(mixins.ListModelMixin,                                            
              mixins.RetrieveModelMixin,                                        
              mixins.CreateModelMixin,                                          
              mixins.UpdateModelMixin,                                        
              ):       
Run Code Online (Sandbox Code Playgroud)

django-rest-framework openapi drf-yasg

11
推荐指数
2
解决办法
1551
查看次数

验证 Swagger API 文档 (drf-yasg)

我已经设置了DRF-YASG,但无法弄清楚如何配置它以显示需要身份验证的视图。

下面是配置。

  schema_view = get_schema_view(
      openapi.Info(
        title="Swagger Doc",
        default_version='v1',
        description="Test description",
        terms_of_service="https://www.google.com/policies/terms/",
        contact=openapi.Contact(email="contact@snippets.local"),
        license=openapi.License(name="BSD License"),
      ),
      validators=['flex', 'ssv'],
      permission_classes=(permissions.AllowAny,),  # If I change the permission it throws an exception. See below
      public=False,
      patterns=public_apis,
  )
Run Code Online (Sandbox Code Playgroud)

这些public_apis是我希望人们在通过身份验证后看到的 API。

使用上述配置,它不会显示单个 API。它只显示AuthorizeButton 和显示No operations defined in spec!. 但是如果我更改public=Falsepublic=True那么它会显示所有的 API。

PS:之前我使用的是Django Rest Swagger,并且我已经能够将它配置为仅在提供 JWT 令牌后才显示 API。

我正在使用 JWT 进行身份验证。

权限变更的例外:

另一个问题是,如果我将上面的权限更改为 DRF 权限类,渲染将失败并显示以下错误:

  Internal Server Error: /swagger/
  Traceback (most recent call …
Run Code Online (Sandbox Code Playgroud)

django django-rest-framework drf-yasg

9
推荐指数
1
解决办法
7764
查看次数

DRF - `write_only=True` 显示在响应模式中

我正在使用drf-yasg来记录我的 API。但是,我遇到了一个问题

我有一个序列化程序,其中一个字段设置为write_only=True.

class XYZSerializer(serializers.ModelSerializer):
    status = serializers.BooleanField(default=True, write_only=True)

    class Meta:

        model = XYZ
        fields = ('id', 'status')
Run Code Online (Sandbox Code Playgroud)

生成 swagger 文档时,字段status仍显示在响应字段中。从技术上讲,它不应该。

如何纠正这个?

python swagger django-rest-framework drf-yasg

8
推荐指数
1
解决办法
637
查看次数

DRF YASG 定制

我正在尝试使用 yasg 自定义我的 api 文档 buuild。

首先,我想确定我自己的部分的命名,以及该部分中应包含哪些端点。似乎节的命名是基于不属于最长公共前缀的第一个前缀,例如:

如果我们有 url api/v1/message 和 api/v1/test ,那么这些部分将被命名为 message 和 test。有没有办法让我确定此部分的自定义命名?

而且每个章节的介绍都是空的,如何在这里添加文字呢? 如何在此处添加文字?

最后但并非最不重要的一点是,Stripe 有这些令人惊叹的部分分隔符,我如何将它们添加到 drf yasg 中。

部分分隔线

django documentation django-rest-framework openapi drf-yasg

8
推荐指数
1
解决办法
5240
查看次数

如何使用 drf-yasg 自动生成的 swagger 页面配置“HTTPS”方案?

我知道在传统的 swagger YAML 文件中,我们可以定义方案:

schemes:
  - http
  - https

//OR

schemes: [http, https]
Run Code Online (Sandbox Code Playgroud)

但是,如何使用drf-yasg库自动生成的 swagger 页面做同样的事情?

现在,生成的 swagger 页面仅包含HTTP方案,但HTTPS已丢失。我试过将DEFAULT_API_URLin设置setting.pyhttps://mybaseurl.com,但它似乎不起作用。

python python-3.x swagger django-rest-framework drf-yasg

8
推荐指数
2
解决办法
2956
查看次数

用于 GET 请求的 drf-yasg 文档输入和输出序列化程序

我想用 drf-yasg 记录 GET 请求的输入模式和输出模式。

似乎并不容易。

     @swagger_auto_schema(
         manual_parameters=[
             openapi.Parameter('cart_id', in_=openapi.IN_QUERY,
                               type=openapi.TYPE_INTEGER)
         ])
Run Code Online (Sandbox Code Playgroud)

上面的代码显示了 GET 参数,但以某种方式隐藏了响应模式。

@swagger_auto_schema(methods=['put', 'post'], request_body=UserSerializer)
Run Code Online (Sandbox Code Playgroud)

我不能将 request_body 用于 GET 查询参数,它仅用于帖子正文

那么如何使用 drf-yasg 记录我的输入模式和输出模式?

django-rest-framework drf-yasg

8
推荐指数
1
解决办法
2824
查看次数

Django,drf-yasg - 如何为标签添加描述?

Swagger 文档说你可以这样做:

https://swagger.io/docs/specification/grouping-operations-with-tags/

但不幸的是 drf-yasg 没有实现这个功能:

https://github.com/axnsan12/drf-yasg/issues/454

据说,我可以添加自定义生成器类,但这是一个非常笼统的答案。现在我看到了drf_yasg.openapi.Swaggerget infoblock 并且我有想法,这可能是将全局tags部分作为附加 init 参数的正确位置,但它比自定义生成器类更深入,而且我对此模块缺乏了解

有没有人有解决这个特定问题的方法,或者至少可能是某种教程的链接,如何正确自定义生成器类?

python django-rest-framework openapi drf-yasg

8
推荐指数
2
解决办法
3411
查看次数

drf-spectacular 使用错误的 AutoSchema 来生成 Swagger

以前我正在使用drf-yasg但想要更新以使用 OpenAPI 3。我正在尝试切换到drf-spectacular. 按照说明,我跑了pip install drf-spectacular,我已经删除了对drf-yasg包的所有引用,并更新Settings.py如下:

INSTALLED_APPS = [ 
    ...
    "drf_spectacular",
]


REST_FRAMEWORK = {
    "DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
}
Run Code Online (Sandbox Code Playgroud)

当我使用 CLI 生成架构时,我得到以下AssertionError. 如果有人以前遇到过这个问题并有任何见解,将不胜感激!

我使用的是 Python 3.7、Django 3.0、Django Rest Framework 3.11 和 DRF Spectacular 0.10.0。

Traceback (most recent call last):
  File "manage.py", line 23, in <module>
    main()
  File "manage.py", line 19, in main
    execute_from_command_line(sys.argv)
  File "/opt/anaconda3/envs/dev/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/opt/anaconda3/envs/dev/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv) …
Run Code Online (Sandbox Code Playgroud)

django swagger django-rest-framework drf-yasg

8
推荐指数
1
解决办法
1322
查看次数

Django Rest框架drf-yasg swagger ListField序列化器的多个文件上传错误

我正在尝试从swagger(使用drf-yasg)进行上传文件输入,但是当我使用MultiPartParser类时,它给了我以下错误:

drf_yasg.errors.SwaggerGenerationError: FileField is supported only in a formData Parameter or response Schema
Run Code Online (Sandbox Code Playgroud)

我的看法:

drf_yasg.errors.SwaggerGenerationError: FileField is supported only in a formData Parameter or response Schema
Run Code Online (Sandbox Code Playgroud)

我的序列化器:

class AddExperience(generics.CreateAPIView):
    parser_classes = [MultiPartParser]

    permission_classes = [IsAuthenticated]
    serializer_class = DoctorExperienceSerializer
Run Code Online (Sandbox Code Playgroud)

我也尝试过FormParser,但它仍然给我同样的错误。另外:FileUploadParser解析器但它的工作原理如下JsonParser

python django python-3.x django-rest-framework drf-yasg

7
推荐指数
1
解决办法
1768
查看次数