POSTing to an Action / Detail route on a Viewset

18 views
Skip to first unread message

Aaron

unread,
Apr 10, 2020, 4:21:03 AM4/10/20
to Django REST framework
I'd like to POST to a detail route on my Viewset:

class FooViewSet(
mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet
):

permission_classes = [IsAuthenticated]
queryset = models.Foo.active_objects.all()
serializer_class = serializers.FooSerializer

@action(
methods=["post"],
detail=True,
permission_classes=[IsAuthenticated],
url_path="",
url_name="purchase",
)
def do_foobar(self, request, pk=None):
pass

EG: POST /foo/<fooID>/


I'm aware that ViewSets allow for the @action decorator:


@action( methods=["post"], detail=True, permission_classes=[IsAuthenticated], url_path="", url_name="foo", )


This however doesn't seem to allow the `url_path` field to be blank, so for example this works:


@action( methods=["post"], detail=True, permission_classes=[IsAuthenticated], url_path="foo", url_name="foo", )



But then requires my API to be structured like POST /store/<storeID>/foo



Is there a way to use an empty `url_path`? because just putting an empty string doesn't work.


Reply all
Reply to author
Forward
0 new messages