我正在尝试构建一个前端,它将检查可用于显示不同UI元素的选项(如编辑按钮)。我可以从我的django rest框架后端中提取OPTIONS,但它显示的唯一操作是POST。
我正在使用带有DjangoObjectPermissions的ModelViewSet。
我收到此作为回复标题:
Access-Control-Allow-Headers:accept, accept-encoding, authorization, content-type, dnt, origin, user-agent, x-csrftoken, x-requested-with, access-control-allow-methods
Access-Control-Allow-Methods:DELETE, GET, OPTIONS, PATCH, POST, PUT
Access-Control-Allow-Origin:*
Access-Control-Max-Age:86400
Allow:GET, POST, HEAD, OPTIONS
Content-Length:405
Content-Type:application/json
Date:Wed, 02 Aug 2017 19:40:08 GMT
Server:WSGIServer/0.1 Python/2.7.12
Vary:Accept
X-Frame-Options:SAMEORIGIN
这是json发回的:
{
"name":"Project List",
"description":"",
"renders":[
"application/json",
"text/html"
],
"parses":[
"application/json",
"multipart/form-data"
],
"actions":{
"POST":{
"id":{
"type":"integer",
"required":false,
"read_only":true,
"label":"ID"
},
"description":{
"type":"string",
"required":true,
"read_only":false,
"label":"Description"
},
"name":{
"type":"string",
"required":true,
"read_only":false,
"label":"Name",
"max_length":80
}
}
}
}
我试图从响应标题中读取允许的操作,但事实证明这是不可能的。有没有办法让Django Rest Framework显示用户的所有可用操作?
答案 0 :(得分:3)
默认情况下DRF renders options only for POST
and PUT
methods。如果要将此行为扩展到更多HTTP方法,则需要创建tput rmcup
的子类并覆盖SimpleMetadata
方法。