Typically, in a DRF Viewset you might do something like this:
class FooViewSet(viewsets.ViewSet):
"""
Foo-related viewsets.
"""
permission_classes = [IsAuthenticated,]
def list(self, request):
"""
A list of foo objects.
"""
context = {'request': self.request}
queryset = Foo.objects.all()
serializer = FooSerializer(queryset, many=True, context=context)
return Response(serializer.data)
def retrieve(self, request, pk=None):
"""
Get one publicly available Foo item.
"""
context = {'request': self.request}
queryset = Foo.objects.all()
store_object = get_object_or_404(queryset, pk=pk)
serializer = FooSerializer(store_object, context=context)
return Response(serializer.data)
This works fine, and respectively correlates to:
GET /foo and GET /foo/<pk>. However, the last endpoint I need is POST /foo/<pk>. The problem here is that providing a create method to the views typically will be routed to POST /foo. Is there anything neat and elegant I can do from the ViewSet itself? Or is the only option basically to route POST /foo/<pk> to a specific one-off view?