夸张的API文档

2024-10-05 14:25:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我看到了FlaskDjango的夸张文档。在烧瓶中,我可以设计和记录我的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编写原始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',)

url.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字符串的字段,就像thisapi那样?

谢谢

我可以通过硬编码我的api在Flask中完成所有这些事情。但在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'
            ]
        },

Tags: djangonamepyidjsontrueurlflask
1条回答
网友
1楼 · 发布于 2024-10-05 14:25:01

这行得通吗:

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"}

相关问题 更多 >