Reputation: 1605
I can't apply the @action
decorator to create
function within a standard views.Viewset
subclass.
@action(detail=True) # This fails.
def create(self, request, pk=None):
return Response(etc...)
On other methods, adding that decorator allows me to create URLs easily with <pk>
within the URL. For example, I can send a POST
request to http://main_url.com/do_something_to_user_with_id/7/
, with do_something_to_user_with_id
being some random function within the viewset and 7
being the user id/primary key.
Is there any way I can do this with create
?
Upvotes: 0
Views: 1489
Reputation: 1632
ViewSet
provides following six actions - list, create, retrieve, update, partial_update and destroy
- here
So you don't need an action
decorator to implement a route for the create
method as its a default route for post
calls as mentioned here -
A ViewSet class is simply a type of class-based View, that does not provide any method handlers such as .get() or .post(), and instead provides actions such as .list() and .create().
So if you have any method other than the six method names listed above that you want to be routable, then you can use the @action
decorator. -here
If you have ad-hoc methods that should be routable, you can mark them as such with the @action decorator. Like regular actions, extra actions may be intended for either a single object, or an entire collection. To indicate this, set the detail argument to True or False.
Upvotes: 1
Reputation: 188
if you know about django-restframework then you can do something like
from rest_framework.decorators import api_view
@api_view(['GET'])
def create(self, request, pk=None):
return Response(etc...)
Upvotes: 0