2014-01-30 103 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

請看我編輯的問題。我添加了我的模型,序列化器,網址和視圖。 –

相關問題