当我尝试从对象中获取价值时,它给我错误AttributeError: 'dict' object has no attribute 'usertype'
,我可以看到我正在获取正确的queryset结果,但无法从中获取值,我的queryset <QuerySet [{'usertype': 2}]>
,这是我的完整代码,任何人都可以帮助我解决其中的问题吗?
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[0].usertype)