【问题标题】:Django Modelviewset try to create custom routeDjango Modelviewset 尝试创建自定义路由
【发布时间】:2021-11-26 18:41:56
【问题描述】:
@action(detail=False, methods=['GET'], name='Get Vesting Locaitons')
def get_vesting_locations(self, request, pk=None, *args, **kwargs):

我正在尝试返回 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/

【问题讨论】:

    标签: django django-rest-framework django-views django-urls


    【解决方案1】:

    detail=False 定义action 将告诉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):
    

    【讨论】:

      猜你喜欢
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多