Reputation: 3155
I would like to extend the standard ListAPIView
by adding allowed actions to each item so that the JSON returned on GET /books/
would approximately look as follows:
[
{
"ID": 1,
"name": "Animal Farm",
"author": "George Orwell",
"actions": [
"detail": {
"method": "GET"
"uri": "/books/1"
},
"remove": {
"method": "DELETE"
"uri": "/books/1"
}
...
]
},
...
]
By having such an "action list", I can easily tell the frontend which actions are currently allowed so it can e. g. include only buttons for those actions.
I went through the DRF docs and could not find a similar functionality. Shall I write it from scratch or is there a 3rd party plugin that could possibly help me? And when writing this from scratch, how would you design (=where to write the code of) such a feature?
Upvotes: 0
Views: 922
Reputation: 2332
I'm not sure, if there is such plugin in DRF (maybe something for serializers?).
If writing from scratch, you should override your get()
method ListAPIView
(or list()
method of mixins.ListModelMixin
)
Upvotes: 0
Reputation: 1272
DRF has support for that when you use the OPTIONS
method, you can find more info here.
You could take a look into how this SimpleMetadata
function creates the list of actions and either base yourself on it to write yours from scratch, or just find a way to call it to render the action list.
Upvotes: 1