简体   繁体   English

Django Rest 框架 Swagger 文档

[英]Django Rest Framework Swagger Documentation

Good day everyone!今天是个好日子! I am writing a game application using Django and Django rest framework.我正在使用 Django 和 Django rest 框架编写游戏应用程序。 I have been working on swagger documentation and the main problem is, that autogenerated schema is without any params.我一直在研究 swagger 文档,主要问题是,自动生成的模式没有任何参数。 Here are examples of my realization:以下是我的实现示例:

Tempaltes.html模板.html

  {% load static %}
<!DOCTYPE html>
<html>
  <head>
    <title>School Service Documentation</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="//unpkg.com/swagger-ui-dist@3/swagger-ui.css" />
  </head>
  <body>
    <div id="swagger-ui"></div>
    <script src="//unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js"></script>
    <script>
    const ui = SwaggerUIBundle({
        url: "{% url schema_url %}",
        dom_id: '#swagger-ui',
        presets: [
          SwaggerUIBundle.presets.apis,
          SwaggerUIBundle.SwaggerUIStandalonePreset
        ],
        layout: "BaseLayout"
      })
</script>

Urls.py网址.py

urlpatterns = [

    path('api_schema/', get_schema_view(
        title='API Schema',
        version="3.0.0",
        description='Guide for the REST API',
    ),
         name='api_schema'),
    path('docs/', TemplateView.as_view(
        template_name='index.html',
        extra_context={'schema_url': 'api_schema'}
    ), name='swagger-ui'),
    path('admin/', admin.site.urls),
    path('game/', include('GameWorld.urls'))
]

Views.py视图.py

class EventDetailsView(GenericAPIView):
    serializer_class = serializers.EventDetailsSerializer
    """
    hard_token
    event_type
    event_id =
    """
    # schema = ManualSchema(fields=[
    #     coreapi.Field(
    #         "hard_token",
    #         required=True,
    #         location="query",
    #         schema=coreschema.String()
    #     ),
    #     coreapi.Field(
    #         "event_type",
    #         required=True,
    #         location="query",
    #         schema=coreschema.String()
    #     ),
    #     coreapi.Field(
    #         "event_id",
    #         required=True,
    #         location="query",
    #         schema=coreschema.Integer()
    #     ),
    # ])

    @auth
    def post(self, request, *args, **kwargs):
        event_id = request.data.get('event_id')
        for_user_events = engine_models.Event.objects.filter(owner__pk=request.user.pk)
        event = for_user_events.get(pk=event_id)
        return Response(event.data)

As you can see in the post.正如你在帖子中看到的那样。 I also tried to rewrite it manually using Manual schema according to the docs but the result was not successful.我还尝试根据文档使用手动模式手动重写它,但结果不成功。

Image: SwaggerUI图片: SwaggerUI

Please, can you help me with this task.拜托,你能帮我完成这个任务吗? Appreciated, have a great week!欣赏,祝您度过愉快的一周!

You can use these:你可以使用这些:

import coreapi

    schema = AutoSchema(
            manual_fields=[
            coreapi.Field("my", required=True,location='query',description='City name or airport code.'),
            coreapi.Field("my1", required=True,location='query',description='City name or airport code.'),
            coreapi.Field("my2", required=True,location='query',description='City name or airport code.'),
        ])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM