Reputation: 1673
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)
Upvotes: 0
Views: 448
Reputation: 88689
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
print(user_profile[0]['usertype']) # this will print data from the first item
for item in user_profile:
print(item['usertype'])
Upvotes: 1