代码之家  ›  专栏  ›  技术社区  ›  М.Б.

Django Rest框架中带APIViews的嵌套路由

  •  1
  • М.Б.  · 技术社区  · 8 年前

    Profiles Albums , Images , Comments , Likes .

    我试图通过以下方式访问资源: api/v1/profiles/1/albums --&燃气轮机;从个人资料中获取id为1的所有相册

    ViewSet 而是使用了 APIView 我想用它(我真的是新手,我甚至不知道我能用它吗 用于CRUID操作)。 我已尝试实现以下内容:

    还有很多其他的,但我不能让它工作。。。

    如果有一些详细的教程,请参考它,我真的需要很快完成这一点。

    谢谢大家!

    2 回复  |  直到 4 年前
        1
  •  3
  •   М.Б.    4 年前

    我用 get_queryset get_object . 其他寻求帮助的人有一个完整的例子:

    URL。py公司 :

    url(r'(?P<profile_id>\d+)/albums/(?P<album_id>\d+)/images/(?P<image_id>\d+)/comments/(?P<comment_id>\d+)/?$',CommentDetailAPIView.as_view(), name='profile-album-image-comment'),
    

    在里面 意见。py公司 :

    class GetCommentsAPI(ListAPIView):
    """
    
    """
    serializer_class = CommentSerializer
    filter_backends = [SearchFilter]  # this must be array!
    authentication_classes = [AllowAny]
    
    def get_queryset(self, *args, **kwargs):
       # ipdb.set_trace(context=5)
        profile_id = self.kwargs.get("profile_id")
        if not profile_id:
            return Response({"status": "fail"}, status=403)
    
        profile = Profile.objects.get(pk=profile_id)
    
        album_id = self.kwargs.get("album_id")
        if not album_id:
            return Response({"status": "fail"}, status=403)
        album = Album.objects.get(pk=album_id, owner_id=profile_id)
    
        if not album:
            return Response({"status": "fail"}, status=404)
    
        image_id = self.kwargs.get("image_id")
        if not image_id:
            return Response({"status": "fail"}, status=403)
        image = Image.objects.get(pk=image_id, album_id=album_id)
    
        queryset_list = Comment.objects.filter(image__pk=image_id)
        return queryset_list
    
    class CommentDetailAPIView(DestroyModelMixin, UpdateModelMixin, RetrieveAPIView):
    """
    
    """
    queryset = Comment.objects.all()
    serializer_class = DetailedCommentSerializer
    permission_classes = [IsAuthenticatedOrReadOnly]
    
    def get_object(self):
        profile_id = self.kwargs.get("profile_id")
        profile = Profile.objects.get(pk=profile_id)
        if not profile:
            return JsonResponse({"status":"fail","code":404})
    
        album_id = self.kwargs.get("album_id")
        album = Album.objects.get(pk=album_id)
        if not album:
            return JsonResponse({"status": "fail", "code": 404})
    
        image_id = self.kwargs.get("image_id")
        image = Image.objects.get(pk=image_id)
        if not image:
            return JsonResponse({"status": "fail", "code": 404})
    
        comment_id = self.kwargs.get("comment_id")
        if not comment_id:
            return JsonResponse({"status": "fail", "code": 404})
    
        comment = get_object_or_404(queryset=Comment.objects.all(), pk=comment_id, image__pk=image_id)
        return comment
    
    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)
    
    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)
    


    https://github.com/milosb793/django-gallery-api

        2
  •  1
  •   Brown Bear    8 年前

    也许你需要 extra-link-and-actions ,例如:

    from rest_framework.decorators import detail_route
    
    class ProfileView
        # Your Code Here
    
        @detail_route(methods=['GET'])
        def albums(request, pk=None):
            # Heed to change related name 'albums_set'
            qs = self.get_object().albums_set.all()
            serializer = AlbumsSerializer(qs, many=True)
            return Response(serializer.data)