DRF:如何在使用@detail_route 时移动 URL 末尾的查找字段

DRF: How to move lookup field at the end of URL while using @detail_route

我正在尝试创建一个@detail_route,但是默认路由器的 URL 是 /api/v1/stores/section/{id}/items/

但我想像这样在 URL 的末尾移动查找字段 /api/v1/stores/section/items/{id}/

class MyViewSet(viewsets.ViewSet):
    serializer_class = TestSerializer

    @detail_route(methods=['get'])
    def items(self, request, pk=None):

如果不将特定的 url 与 Django url 模式作为 shown in the tutorial or by altering the router's code 来处理这种情况,您将无法做到这一点。

@action(detail=True, url_path='/(?P<extra_id>[^/.]+)/')
def items(self, request, extra_id):

添加这个!

警告:

如果错误有效,请删除“/”

您可能需要使用 'extra_id'

查询项目对象