Django Modelviewset 尝试创建自定义路由

Django Modelviewset try to create custom route

@action(detail=False, methods=['GET'], name='Get Vesting Locaitons')
def get_vesting_locations(self, request, pk=None, *args, **kwargs):

我正在尝试 return json 响应并收到 404 错误

这是路由注册器

router.register(r'vesting', VestingViewSet, basename='vesting')

这些是我试图获取的网址

http://localhost:8000/vesting/get_vesting_locations/617b8bd8-6fdd-43eb-948a-4b17d1a0a089/
http://localhost:8000/vesting/617b8bd8-6fdd-43eb-948a-4b17d1a0a089/get_vesting_locations/

detail=False 定义 action 将告诉 url 此视图不适用于单个对象。所以它将构建这个 url:

vesting/get_vesting_locations/

所以想去:

vesting/617b8bd8-6fdd-43eb-948a-4b17d1a0a089/get_vesting_locations/

然后会给你一个 404。

为了使该动作与单个对象一起工作并支持上述 url,请设置 detail=True:

@action(detail=True, methods=['GET'], name='Get Vesting Locaitons')
def get_vesting_locations(self, request, pk=None, *args, **kwargs):