2014-01-30 104 views
1

我看到了FlaskDjango的swagger文档。在Flask中,我可以设计并记录我手动编写的API(包括参数部分所需的字段,可选字段等)。Swagger 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 in 不好。我的主要问题是如何在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') 

serializers.py

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

urls.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()), 
) 

views.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)安静类似于第一,如果我需要添加它接受值的B/W 3设置值的字段,即一个下拉菜单。我如何添加它?

3)如何添加一个可选字段? (因为在请求PUT的情况下,我可能只更新1个字段,其余为空,表示optional字段)。

4)另外我该如何添加一个接受json字符串的字段,如this api所做的那样?

感谢

我可以用我的硬编码的API做所有这些事情瓶。但是在Django中,它会从我的模型中自动执行,而不是(我相信)可以让我访问自定义我的api。在Flask中,我只需要用手编写API,然后与Swagger集成即可。 在Django中是否存在同样的事情?

像我只需要添加以下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' 
      ] 
     }, 
+0

[此](http://stackoverflow.com/questions/34591291/show-maximum-minimum-和-default-in-swagger-for-django-rest-framework/37922613#37922613)答案可能对您有用 – Marosinho

回答

0

将这工作:

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

感谢您的回复。我将如何去“放”请求?我现在要做的就是包括像'类ActionSerializer(serializers.ModelSerializer)的'Serializer'类选择字段: \t类元: \t \t模型=控制器 \t \t栏=( 'installation_id', 'IP',” xyz','abc')'这样所有的字段都被列出来,但是没有一个是**可选的**,每个字段都是**必需的**。 ** 1)**我如何添加可选字段? ** 2)**另外我如何添加一个接受'json'字符串的字段。? ** 3)**我如何添加接受值的字段b/w 3提供的值,即下拉列表。提前致谢。 –

+0

@ python-coder请把你所有的细节问题,模型定义,序列化,查看,网址.. – mariodev

+0

请看我编辑的问题。我添加了我的模型,序列化器,网址和视图。 –

相关问题