Ale*_*xis 10 python django-rest-framework
我正在使用django-rest-framwork和django-rest-swagger.
问题是我直接从请求正文中获取数据:
def put(self, request, format=None):
"""
This text is the description for this API
username -- username
password -- password
"""
username = request.DATA['username']
password = request.DATA['password']
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试来自swagger-ui的请求时,我无法指定"参数类型"(默认情况下是查询,无法找到从docstring更改它的方法)
我已经设法通过从文件"introspectors.py"更改函数build_query_params_from_docstring中的某一行来解决我的问题,但我想知道是否还有其他方法可以做到这一点.
更新:此答案仅适用于django-rest-swagger <2,请参阅下面@krd的评论.
文档:http://django-rest-swagger.readthedocs.org/en/latest/yaml.html
如果你想放置表单数据:
def put(self, request, format=None):
"""
This text is the description for this API.
---
parameters:
- name: username
description: Foobar long description goes here
required: true
type: string
paramType: form
- name: password
paramType: form
required: true
type: string
"""
username = request.DATA['username']
password = request.DATA['password']
Run Code Online (Sandbox Code Playgroud)
对于JSON主体,您可以执行以下操作:
def put(...):
"""
...
---
parameters:
- name: body
description: JSON object containing two strings: password and username.
required: true
paramType: body
pytype: RequestSerializer
"""
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12228 次 |
| 最近记录: |