0

I am working on login api, when i tried to run this command
user_profile = UserProfile.objects.filter(user_id=user.id).values('usertype') It gives me this error : AttributeError: 'QuerySet' object has no attribute 'usertype' but in console i checked i am getting its queryset <QuerySet [{'usertype': 2}]> but not able to fetch the value from it, can anyone please help me how to resolve this issue ? here is my signin function of it

class Login(APIView):
    permission_classes = (AllowAny,)

    def post(self, request):
        username = request.data.get("username")
        password = request.data.get("password")
        if username is None or password is None:
            return Response({'success': False, 'error': 'Please provide both username and password'},
                            status=HTTP_400_BAD_REQUEST)
        user = authenticate(username=username, password=password)
        # return Response({'success': True, 'user': user},status=HTTP_200_OK)
        if not user:
            return Response({'success': False, 'error': 'Invalid Credentials'},
                            status=HTTP_400_BAD_REQUEST)
        access_token, refresh_token = utils.generate_tokens(user)


        user_profile = UserProfile.objects.filter(user_id=user.id).values('usertype')

        print(user_profile.usertype)

1 Answer 1

1

user_profile is a QuerySet object, a list like iterable. If you want to access the usertype, you should either use array index or a loop

Method-1

print(user_profile[0]['usertype']) # this will print data from the first item

Method-2

for item in user_profile:
    print(item['usertype'])
Sign up to request clarification or add additional context in comments.

1 Comment

It gives me this error now : AttributeError: 'dict' object has no attribute 'usertype'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.