I have a Game
model and am doing the corresponding REST routes for it, e.g. GET /game
, GET /game/1
, etc.
I only want API consumers to GET existing games. I don't want them to be able to POST new games arbitrarily. Rather, they should have to go through a specal route, POST /game/upload_schedule
for this.
I have the following:
class GameViewSet(viewsets.ModelViewSet):
queryset = Game.objects.all()
serializer_class = GameSerializer
http_method_names = ['get', 'head']
@list_route(methods=['post'])
def upload_schedule(self, request):
return Response(["foo"])
However, when I POST /game/upload_schedule
, I get a method not allowed error. The reason is that http_method_names
prevents it from happening. If I change it to the following:
http_method_names = ['get', 'head', 'post']
Then the POST /game/upload_schedule
route works. However, now so does POST /game
!
How do I proceed?