Siner
Siner

Reputation: 531

Django Viewset detail supports only with pk? not args?

My ACTION view is not working.

How to pass args not only pk?


I was using function based api like

Function Based Api View (1)

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

Now, I have new viewset for model Food.

ViewSet (2)

{host}:{port}/food/

I hope to integrate this apis to one ModelViewSet so i did like below.

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)

Upvotes: 1

Views: 1147

Answers (1)

Siner
Siner

Reputation: 531

Sorry, I clear this problem with query url like below.

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')

Upvotes: 1

Related Questions