Swagger API文档

vqlkdk9b  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(190)

我看到了FlaskDjango的文档。在Flask中,我可以设计和记录我的API手写。(包括哪些字段是必需的,可选的等参数部分)。
"这就是我们在弗拉斯克的做法“

class Todo(Resource):
    "Describing elephants"
    @swagger.operation(
        notes='some really good notes',
        responseClass=ModelClass.__name__,
        nickname='upload',
        parameters=[
            {
              "name": "body",
              "description": "blueprint object that needs to be added. YAML.",
              "required": True,
              "allowMultiple": False,
              "dataType": ModelClass2.__name__,
              "paramType": "body"
            }
          ],
        responseMessages=[
            {
              "code": 201,
              "message": "Created. The URL of the created blueprint should be in the Location header"
            },
            {
              "code": 405,
              "message": "Invalid input"
            }
          ]
        )

我可以选择包含哪些参数,哪些不包含。但是我如何在Django中实现相同的参数呢?Django-Swagger Document一点都不好。我的主要问题是我如何在Django中编写raw-json。
在Django中,它自动化了,不允许我自定义我的json。我如何在Django上实现同样的事情?
下面是models.py文件

class Controller(models.Model):
    id = models.IntegerField(primary_key = True)
    name = models.CharField(max_length = 255, unique = True)
    ip = models.CharField(max_length = 255, unique = True)
    installation_id = models.ForeignKey('Installation')

序列化程序.py

class ActionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Controller
        fields = ('installation',)

网址.py

from django.conf.urls import patterns, url
from rest_framework.urlpatterns import format_suffix_patterns
from modules.actions import views as views

urlpatterns = patterns('',
    url(r'(?P<installation>[0-9]+)', views.ApiActions.as_view()),
)

查看次数.py

class ApiActions(APIView):

    """
    Returns controllers List
    """

    model = Controller
    serializer_class = ActionSerializer 

    def get(self, request, installation,format=None):

        controllers = Controller.objects.get(installation_id = installation)
        serializer = ActionSerializer(controllers)
        return Response(serializer.data)

我的问题是

**1)**如果我需要添加一个字段,例如xyz,但该字段不在我的模型中,我该如何添加?
2)安静类似于1st,如果我需要添加一个字段,接受值B/w 3提供的值,即下拉列表。我如何添加它?
**3)**我如何添加可选字段?(因为在PUT请求的情况下,我可能只更新1个字段,其余的字段为空,这意味着optional字段)。
**4)**另外,我如何添加一个接受json字符串的字段,就像这个api所做的那样?

谢谢
我可以在Flask中通过硬编码我的API来完成所有这些事情。但是在Django中,它从我的模型自动化,这并不(我相信)给我定制我的API的权限。在Flask中,我只需要手工编写我的API,然后与Swagger集成。Django中也有同样的东西吗?
就像我只需要在我的Flask代码中添加下面的json,它就会回答我所有的问题。


# Swagger json:

    "models": {
        "TodoItemWithArgs": {
            "description": "A description...",
            "id": "TodoItem",
            "properties": {
                "arg1": { # I can add any number of arguments I want as per my requirements.
                    "type": "string"
                },
                "arg2": {
                    "type": "string"
                },
                "arg3": {
                    "default": "123",
                    "type": "string"
                }
            },
            "required": [
                "arg1",
                "arg2" # arg3 is not mentioned and hence 'opional'
            ]
        },
jk9hmnmh

jk9hmnmh1#

这是否可行:

class TriggerView(APIView):
    """
    This text is the description for this API
        mykey -- My Key parameter
    """

    authentication_classes = (BasicAuthentication,)
    permission_classes = (IsAuthenticated,)

    def post(self, request, format=None):
        print request.DATA
        return Response(status=status.HTTP_202_ACCEPTED)

POST请求:

Authorization:Basic YWRtaW46cGFzcw==
Content-Type:application/json

{"mykey": "myvalue"}

相关问题