为什么我在登录时得到 'ImageFieldFile' object has no attribute 'pk'?
Why am I getting 'ImageFieldFile' object has no attribute 'pk' when I login?
我正在使用 django-rest-auth
和 REST 框架 JWT Auth 在 Django-Rest_framework
中构建后端身份验证,当我登录时,我不断收到此错误。我做错了什么?
AttributeError at /api/auth/login/
'ImageFieldFile' object has no attribute 'pk'
Request Method: POST
Request URL: http://127.0.0.1:8000/api/auth/login/
Django Version: 3.2.8
Exception Type: AttributeError
Exception Value:
'ImageFieldFile' object has no attribute 'pk'
Exception Location: D:\Python Workspace\Rest_Gama\venv\lib\site-packages\rest_framework_jwt\utils.py, line 86, in jwt_create_payload
Python Executable: D:\Python Workspace\Rest_Gama\venv\Scripts\python.exe
Python Version: 3.9.7
这是我的models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
profile = models.ImageField(default='user_default_m.png', upload_to='profile/', blank=True)
dob = models.DateField(auto_now=False, auto_now_add=False, blank=True)
这是我的serializers.py
from rest_auth.serializers import LoginSerializer as RestAuthLoginSerializer
from rest_framework import serializers
from users.models import CustomUser
class CustomUserDetailsSerializer(serializers.ModelSerializer):
class Meta:
model = CustomUser
fields = ('pk', 'first_name', 'last_name', 'email', 'profile', 'dob', 'date_joined', 'last_login')
read_only_fields = ('pk', 'email', 'date_joined', 'last_login')
class LoginSerializer(RestAuthLoginSerializer):
username = None
尝试重新安装REST framework JWT Auth
pip install djangorestframework-jwt
参考文档 -> here
我正在使用 django-rest-auth
和 REST 框架 JWT Auth 在 Django-Rest_framework
中构建后端身份验证,当我登录时,我不断收到此错误。我做错了什么?
AttributeError at /api/auth/login/
'ImageFieldFile' object has no attribute 'pk'
Request Method: POST
Request URL: http://127.0.0.1:8000/api/auth/login/
Django Version: 3.2.8
Exception Type: AttributeError
Exception Value:
'ImageFieldFile' object has no attribute 'pk'
Exception Location: D:\Python Workspace\Rest_Gama\venv\lib\site-packages\rest_framework_jwt\utils.py, line 86, in jwt_create_payload
Python Executable: D:\Python Workspace\Rest_Gama\venv\Scripts\python.exe
Python Version: 3.9.7
这是我的models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
profile = models.ImageField(default='user_default_m.png', upload_to='profile/', blank=True)
dob = models.DateField(auto_now=False, auto_now_add=False, blank=True)
这是我的serializers.py
from rest_auth.serializers import LoginSerializer as RestAuthLoginSerializer
from rest_framework import serializers
from users.models import CustomUser
class CustomUserDetailsSerializer(serializers.ModelSerializer):
class Meta:
model = CustomUser
fields = ('pk', 'first_name', 'last_name', 'email', 'profile', 'dob', 'date_joined', 'last_login')
read_only_fields = ('pk', 'email', 'date_joined', 'last_login')
class LoginSerializer(RestAuthLoginSerializer):
username = None
尝试重新安装REST framework JWT Auth
pip install djangorestframework-jwt
参考文档 -> here