Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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
Python 如何在使用DRF Viewset'时在api末尾添加{id}查找;s作用法_Python_Django_Django Rest Framework - Fatal编程技术网

Python 如何在使用DRF Viewset'时在api末尾添加{id}查找;s作用法

Python 如何在使用DRF Viewset'时在api末尾添加{id}查找;s作用法,python,django,django-rest-framework,Python,Django,Django Rest Framework,基本上,我希望在api的末尾有{id}条注释。 从 到 url.py v1_router = routers.DefaultRouter() v1_router.register(r"articles", ArticleViewSet) urlpatterns = [path("v1/", include(v1_router.urls))] 如何制作如上所述的url?对于仍有兴趣了解我如何解决此问题的人 @action( det

基本上,我希望在api的末尾有{id}条注释。 从

url.py

v1_router = routers.DefaultRouter()
v1_router.register(r"articles", ArticleViewSet)

urlpatterns = [path("v1/", include(v1_router.urls))]

如何制作如上所述的url?

对于仍有兴趣了解我如何解决此问题的人

    @action(
        detail=True,
        methods=["post"],
        url_path="comments/(?P<comment_id>[^/.]+)",
    )
    def comments(self, request, comment_id):
        # you can continue here with rest of the code


你能分享你的
url.py
文件吗?
class ArticlesViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, GenericViewSet
):
    queryset = Articles.objects.order_by("-created")
    serializer_class = ArticlesSerializer

    @action(methods=["delete"], detail=True)
    def comments(self, request, *args, **kwargs):
        # do something
v1_router = routers.DefaultRouter()
v1_router.register(r"articles", ArticleViewSet)

urlpatterns = [path("v1/", include(v1_router.urls))]
    @action(
        detail=True,
        methods=["post"],
        url_path="comments/(?P<comment_id>[^/.]+)",
    )
    def comments(self, request, comment_id):
        # you can continue here with rest of the code

http://localhost:8000/articles/{id}/comments/{id}/