django rest 框架中同一资源的两个端点
Two endpoints for the same resource in django rest framework
我想创建两个端点 /comments/
和 /comments/requests/
或类似的东西。第一个显示您的评论,第二个显示您的待处理评论(人们发送给您的需要您批准的评论)。他们都使用评论模型。我怎样才能在 Django Rest Framework 中实现这一点?
现在,我的观点是
class CommentsListview(APIView):
serializer_class = CommentSerializer
def get(self, request, format=None):
comments, _, _, = Comments.get_comment_users(request.user)
comments_serializer = CommentSerializer(comments, many=True)
return Response({'comments': comments_serializer.data})
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})
我想允许用户去 localhost:8000/comments/
查看他们的评论和 localhost:8000/comments/requests/
查看他们待处理的评论请求。由于我无法弄清楚这一点,唯一的其他解决方案是要求用户使用参数作为标志 /comments/?requests=True
来切换端点的行为,但这似乎很草率。
使用list_route装饰器和通用视图集
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/
将调用 list
方法
/comments/requests/
将调用 requests
方法
另请参阅 GenericViews and ViewSet 文档,这可能会有帮助
我想创建两个端点 /comments/
和 /comments/requests/
或类似的东西。第一个显示您的评论,第二个显示您的待处理评论(人们发送给您的需要您批准的评论)。他们都使用评论模型。我怎样才能在 Django Rest Framework 中实现这一点?
现在,我的观点是
class CommentsListview(APIView):
serializer_class = CommentSerializer
def get(self, request, format=None):
comments, _, _, = Comments.get_comment_users(request.user)
comments_serializer = CommentSerializer(comments, many=True)
return Response({'comments': comments_serializer.data})
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})
我想允许用户去 localhost:8000/comments/
查看他们的评论和 localhost:8000/comments/requests/
查看他们待处理的评论请求。由于我无法弄清楚这一点,唯一的其他解决方案是要求用户使用参数作为标志 /comments/?requests=True
来切换端点的行为,但这似乎很草率。
使用list_route装饰器和通用视图集
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/
将调用list
方法/comments/requests/
将调用requests
方法
另请参阅 GenericViews and ViewSet 文档,这可能会有帮助