2013-06-28 96 views
4

我正在編寫我的第一個REST API(使用django-rest-framework)。選項中的Django-rest-framework文檔API

我添加了URL參數來過濾結果。我的理解是,這些參數的文檔屬於OPTIONS動詞。我的代碼:

class SuburbViewSet(viewsets.ReadOnlyModelViewSet): 
    """ 
    Retrieves the suburbs (20 per page). 
    GET and OPTIONS allowed. 
    """ 
    model = Suburb 
    serializer_class = SuburbSerializer 

    def get_queryset(self): 
     """ 
     Can filter by region_id, ... 
     - using query parameters in the URL. 
     """ 
     queryset = Suburb.objects.all() 
     region_id = self.request.QUERY_PARAMS.get('region_id', None) 
     if region_id is not None: 
      queryset = queryset.filter(region_id=region_id) 
     return queryset 

    def metadata(self, request): 
     ret = super(SuburbViewSet, self).metadata(request) 

     ret['parameters'] = { 
      "page": { 
       "type": "integer", 
       "description": "The page number", 
       "required": False 
      }, 
      "region_id": { 
       "type": "integer", 
       "description": "The region ID to filter the results", 
       "required": False 
      } 
     } 

     return ret 

這是最好的/只有REST的方式去解釋什麼參數是在選項?

關於django-rest-framework,我已經擴展了元數據(自我,請求),這感覺hacky。我錯過了一些內置的方式來設置參數描述?

謝謝!

回答

3

通用視圖已包含響應OPTIONS請求的參數描述。

例如,完成本教程後,您應該能夠發出OPTIONS請求並檢查可用的操作。

請確保將--user選項設置爲現有的用戶/密碼,否則您只能進​​行現成的訪問,並且不會獲得響應的一部分。

bash: curl -X OPTIONS --user amy:amy -v http://127.0.0.1:8000/snippets/ -H 'Accept: application/json; indent=4'; echo 
* About to connect() to 127.0.0.1 port 8000 (#0) 
* Trying 127.0.0.1... connected 
* Server auth using Basic with user 'amy' 
> OPTIONS /snippets/ HTTP/1.1 
> Authorization: Basic YW15OmFteQ== 
> User-Agent: curl/7.22.0 (x86_64-apple-darwin11.2.0) libcurl/7.22.0 OpenSSL/1.0.0e zlib/1.2.5 libidn/1.22 
> Host: 127.0.0.1:8000 
> Accept: application/json; indent=4 
> 
* HTTP 1.0, assume close after body 
< HTTP/1.0 200 OK 
< Date: Fri, 28 Jun 2013 09:27:01 GMT 
< Server: WSGIServer/0.1 Python/2.7.2 
< Vary: Accept, Cookie 
< Content-Type: application/json; indent=4; charset=utf-8 
< Allow: GET, POST, HEAD, OPTIONS 
< 
{ 
    "name": "Snippet List", 
    "description": "This endpoint presents code snippets.\n\nThe `highlight` field presents a hyperlink to the hightlighted HTML\nrepresentation of the code snippet.\n\nThe **owner** of the code snippet may update or delete instances\nof the code snippet.\n\nTry it yourself by logging in as one of these four users: **amy**, **max**,\n**jose** or **aziz**. The passwords are the same as the usernames.", 
    "renders": [ 
     "application/json", 
     "text/html" 
    ], 
    "parses": [ 
     "application/json", 
     "application/x-www-form-urlencoded", 
     "multipart/form-data" 
    ], 
    "actions": { 
     "POST": { 
      "url": { 
       "type": "field", 
       "required": false, 
       "read_only": true 
      }, 
      "highlight": { 
       "type": "field", 
       "required": false, 
       "read_only": true 
      }, 
      "owner": { 
       "type": "field", 
       "required": false, 
       "read_only": true 
      }, 
      "title": { 
       "type": "string", 
       "required": false, 
       "read_only": false, 
       "label": "title", 
       "max_length": 100 
      }, 
      "code": { 
       "type": "string", 
       "required": true, 
       "read_only": false, 
       "label": "code" 
      }, 
      "linenos": { 
       "type": "boolean", 
       "required": false, 
       "read_only": false, 
       "label": "linenos" 
      }, 
      "language": { 
       "type": "multiple choice", 
       "required": true, 
       "read_only": false, 
       "label": "language" 
      }, 
      "style": { 
       "type": "multiple choice", 
       "required": true, 
       "read_only": false, 
       "label": "style" 
      } 
     } 
    } 
} 

您還可以通過可瀏覽的API發出OPTIONS請求。

+0

謝謝,但我仍然不確定GET參數。我看了上面教程中的例子。描述從課堂中檢索,所以我會改變它。我也在框架代碼中發現它:ret ['description'] = get_view_description(self .__ class__)。然而,我不知道如何做的GET參數 - 請參閱我的代碼(region_id)。 –

+2

我使用可瀏覽的API(這是很棒的BTW :))。在你所說的例子中,行動中沒有「獲得」,是由設計嗎? –