Marwan Tushyeh
Marwan Tushyeh

Reputation: 1525

Read Authenticated User Object in ViewSet (Django Rest-Framework)

What i want to do is to filter the queryset for every action on the resource by the authenticated user's ID which is a foreign key on the model, how can I read it when defining the query set?

class RunSessionViewSet(viewsets.ModelViewSet):

    """API endpoint for listing and creating sprints."""
    queryset = RunningSession.objects.order_by('createdDate')
    serializer_class = RunSessionSerializer

Upvotes: 0

Views: 1388

Answers (1)

dnit13
dnit13

Reputation: 2496

You should override get_queryset() method as described in the docs

def get_queryset(self):
    user = self.request.user
    return RunningSession.objects.filter(foreignkey_field=user).order_by('createdDate')

Upvotes: 4

Related Questions