【问题标题】:Django Viewset detail supports only with pk? not args?Django Viewset 详细信息仅支持 pk?不是参数?
【发布时间】:2019-03-04 07:01:43
【问题描述】:

我的 ACTION 视图不起作用。

怎么传args不只是pk?


我使用的是基于函数的 api,例如

基于函数的 API 视图(一)

{host}:{port}/api/food/<year>/<month>/<day>/

现在,我有模型 Food 的新视图。

视图集(2)

{host}:{port}/food/

我希望将这个 api 集成到一个 ModelViewSet,所以我喜欢下面

我想要什么 (1) + (2)

{host}:{port}/food/files/<year>/<month>/<day>/
{host}:{port}/food/

我的代码

class FoodViewSet(viewsets.ModelViewSet):
    queryset = Food.objects.all()
    permission_classes = [blahblah]
    authentication_classes = [blahblah]

    def list(self, request, *args, **kwargs):
        ...
        return Response(blahblah)

    def create(self, request, *args, **kwargs):
        ...
        return Response(blahblah)

    @action(['GET'], detail=True)
    def files(self, request, year, month, day):
        ...
        return Response(blahblah)

【问题讨论】:

    标签: python django api django-rest-framework url-routing


    【解决方案1】:

    对不起,我用下面的查询 url 解决了这个问题。

    网址

    {host}:{port}/food/files/?year=2019&month=02&day=27
    

    我的代码

    @action(['GET'], detail=False)
    def files(self, request):
        year = request.query_params.get('year')
        month = request.query_params.get('month')
        day = request.query_params.get('day')
    

    【讨论】:

      猜你喜欢
      • 2019-12-07
      • 2015-02-12
      • 2017-03-06
      • 2011-11-03
      • 2014-03-11
      • 2022-10-04
      • 2014-04-13
      • 2020-05-27
      • 2014-08-02
      相关资源
      最近更新 更多