[Fixed]-Two endpoints for the same resource in django rest framework

1👍

use list_route decorator and genericviewset

from rest_framework import viewsets
from rest_framework.decorators import list_route

class CommentsListview(viewsets.GenericViewSet):
    serializer_class = CommentSerializer

    def list(self, request, format=None):
        comments, _, _, = Comments.get_comment_users(request.user)
        comments_serializer = CommentSerializer(comments, many=True)
        return Response({'comments': comments_serializer.data})

    @list_route()
    def requests(sel,f request, format=None):
        _, requests, _ = Comments.get_comment_users(request.user)
        requests_serializer = CommentSerializer(requests, many=True)
        return Response({'requests': requests_serializer.data})
  • /comments/ will call list method
  • /comments/requests/ will call requests method

also look at GenericViews and ViewSet docs it might be helpfull

👤aliva

Leave a comment