Django Viewset详细信息仅支持pk?不是args?

问题描述 投票:0回答:1

My ACTION view is not working.

How to pass args not only pk?

我正在使用基于函数的API

Function Based Api View (1)

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

现在,我对模型食品有了新的看法。

ViewSet (2)

{host}:{port}/food/

我希望将这个apis集成到一个ModelViewSet中,所以我在下面做了。

WHAT I WANT (1) + (2)

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

My Code

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个回答
0
投票

对不起,我通过查询网址清除此问题,如下所示。

URL

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

MY CODE

@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')
© www.soinside.com 2019 - 2024. All rights reserved.