[Answered ]-Django REST API get only auth user datas

1👍

You filter with:

@api_view(['GET'])
@permission_classes([IsAuthenticated])
def getTransactions(request):
    transactions = Transaction.objects.filter(account__user=request.user)
    serializer = TransactionSerializer(transactions, many=True)
    return Response(serializer.data)

Here we thus retrieve Transactions for which the account refers to an Account object with request.user as user.


Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.

Leave a comment