[英]How to add {id} lookup at the end of api while using DRF Viewset's action method
基本上,我希望在 api 末尾有 {id} 條評論。來自
http://localhost:8000/articles/{id}/comments/
到
http://localhost:8000/articles/{id}/comments/{id}/
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
網址.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
現在你可以像 api
http://localhost:8000/articles/{id}/comments/{id}/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.