3

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?

Claudiu
  • 224,032
  • 165
  • 485
  • 680

1 Answers1

2

This is an XY Problem. The GameViewSet should only deal with Games and things specifically dealing with games. Uploading the schedule is not a property of a list of games - it is a separate route. So, make it an APIView, separate from the GameViewSet:

class UploadSchedule(APIView):
    def post(self, request):
        raise NotImplementedError()

And then route it up explicitly under ^upload_schedule$.

Claudiu
  • 224,032
  • 165
  • 485
  • 680