Reputation: 125
I'm trying to build a front end that will examine the options available to the use to show different UI elements (like an edit button). I'm able to pull OPTIONS from my django rest framework back-end, but the only action it shows is POST.
I am using ModelViewSet with DjangoObjectPermissions.
I'm receiving this as the response header:
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
Here is the json sent back:
{
"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
}
}
}
}
I have tried to read the allowed actions from the response header but that is proving to be impossible. Is there any way to have Django Rest Framework display all available actions for the user?
Upvotes: 2
Views: 1487
Reputation: 415
By default DRF renders options only for POST
and PUT
methods. If you want to extend this behavior to more HTTP methods you need to create subclass of SimpleMetadata
and override determine_actions(...)
method.
Upvotes: 4