Python 如何在使用DRF Viewset'时在api末尾添加{id}查找;s作用法
基本上,我希望在api的末尾有{id}条注释。 从 到 url.pyPython 如何在使用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
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}/