Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django NestedSimpleRouter不使用路由器中的查找_Django_Django Rest Framework_Drf Nested Routers - Fatal编程技术网

Django NestedSimpleRouter不使用路由器中的查找

Django NestedSimpleRouter不使用路由器中的查找,django,django-rest-framework,drf-nested-routers,Django,Django Rest Framework,Drf Nested Routers,我在url.py中使用了drf嵌套路由器,如下所示 router = SimpleRouter() profile_router = routers.NestedSimpleRouter(router, r'profile', lookup='user') profile_router.register(r'comments', UserCommentViewSet, basename='profile-comments') 视图集是 class UserCommentViewSet(Comm

我在url.py中使用了drf嵌套路由器,如下所示

router = SimpleRouter()
profile_router = routers.NestedSimpleRouter(router, r'profile', lookup='user')
profile_router.register(r'comments', UserCommentViewSet, basename='profile-comments')
视图集是

class UserCommentViewSet(CommentViewSet):
    def get_queryset(self):
        return Comment.objects.filter(owner=self.request.user)
所以URL是这样的

mydomain.com/profile/{profile_id}/comments/
它给了我正确的结果。但是下面的URL也给了我正确的结果

mydomain.com/profile/{anything}/comments/
因为我正在使用会话用户信息过滤数据。有没有可能使URL像

mydomain.com/profile/comments/
提前感谢。

根据您的代码:

router = SimpleRouter()
# router.register('users', UserViewSet, 'user')
profile_router = routers.NestedSimpleRouter(router, r'profile', lookup='user')
profile_router.register(r'comments', UserCommentViewSet, basename='profile-comments')
你的解释是错误的。以下是如何使用NestedSimpleRouter

mydomain.com/profile/ # list all the users.
mydomain.com/profile/{profile_id} # retrieve particular user based on profile_id/user_id/pk.
mydomain.com/profile/{profile_id}/comments/ # list all the comments belong to a particular user (not the current session user) based on profile_id/user_id.
mydomain.com/profile/{profile_id}/comments/{comment_id} # retrieve particular comment based on comment_id.
此url:

mydomain.com/profile/{anything}/comments/
正在工作,因为您正在按
owner=request.user
进行筛选

这个网址:

mydomain.com/profile/{profile_id}/comments/
mydomain.com/profile/comments/
应该通过在
UserCommentViewSet
中获取profile_id来给出所有注释的列表。因此,您的观点如下:

class UserCommentViewSet(CommentViewSet):
    def get_queryset(self):
        return Comment.objects.filter(owner__id=profile_id)
简单地说,您可以使用
NestedSimpleRouter
获取所有用户、用户详细信息、单个用户发布的所有评论和评论详细信息

解决方案:

如果您只需要当前(会话)用户的评论(因为您不需要所有用户的所有评论),您需要以下内容:

router = SimpleRouter()
router.register(r'profile/comments', UserCommentViewSet, basename='profile-comments')
UserCommentViewSet
是:

class UserCommentViewSet(CommentViewSet):
    def get_queryset(self):
        return Comment.objects.filter(owner=self.request.user)
然后,此url:

mydomain.com/profile/{profile_id}/comments/
mydomain.com/profile/comments/

将根据需要提供所有意见。

谢谢您的回答。然而,我试过了,它不起作用。错误消息是“detail”:“Not found”。我刚刚找到原因,“profile”已注册,这就是为什么“profile/something”未找到。你知道如何解决这个问题吗?我没有给profile/什么的代码。这是行不通的。请仔细阅读答案。你不需要所有用户的评论,对吗?您只需要url配置文件/注释中的当前会话用户注释。这就是你在问题中问的。现在不要使用个人资料或其他东西。只需使用个人资料/评论。如果错误仍然存在。你能把所有的错误都贴出来吗?对不起,误解了。我的意思是,我有一个URL/profile和/profile/comments。现在/profile/comments仅在我注释/profile URL时有效。您想在/profile中显示什么?所有用户或当前会话请求。用户?是否考虑过为配置文件创建视图集。并为评论添加@action(detail=False)?