(django) 'utf-8' 编解码器无法解码位置 0 中的字节 0x89:无效的起始字节
(django) 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
我现在正尝试将两个 foreignkey
分配给同一个自定义用户模型,即用户名和个人资料图像字段。
自定义用户模型:
class Account(AbstractBaseUser, PermissionsMixin):
class Meta:
verbose_name_plural = "Account List"
email = models.EmailField(max_length=255, unique=True)
username = models.CharField(max_length=255, unique=True)
name = models.CharField(max_length=255, default="")
profile_image = models.ImageField(max_length=255, upload_to=profile_image_path, blank=True, unique=True)
about = models.TextField(max_length=255, default='Write something about yourself...', blank=True)
start_date = models.DateTimeField(default=timezone.now)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
last_login = models.DateTimeField(auto_now=True)
objects = AccountManager()
USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["username", "name"]
def __str__(self):
return self.username
线程模型:
class Thread(models.Model):
options = (('active', 'Active'), ('deactivated', 'Deactivated'))
username = models.ForeignKey(Account, on_delete=models.CASCADE, to_field='username', related_name='user')
profile_image = models.ForeignKey(Account, on_delete=models.CASCADE, to_field='profile_image', related_name='profile')
alt = models.TextField(max_length=255, blank=True)
image = models.ImageField(max_length=255, upload_to=thread_image_path, default='images/family.png')
content = models.TextField(blank=True)
created = models.DateTimeField(default=timezone.now)
status = models.CharField(max_length=11, choices=options, default='active')
但是,每当我尝试创建线程时,我总是遇到错误:
UnicodeDecodeError at /api/public/thread/
'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
Request Method: GET
Request URL: http://127.0.0.1:8000/api/public/thread/
Django Version: 4.0.4
Exception Type: UnicodeDecodeError
Exception Value:
'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
Exception Location: c:\Users291\Desktop\vscode\my-app\web\env\lib\site-packages\rest_framework\utils\encoders.py, line 50, in default
这个错误发生在我将外键分配给配置文件图像之后,序列化程序无法将图像序列化到 API 端点。我试过 encoders.py
并将编码器更改为 utf16,但问题仍然存在。
有人知道我的代码有什么问题吗?
编辑:
自定义用户serializer.py:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = (
'id',
'email',
'username',
'name',
'profile_image',
'about',
'start_date',
'is_active',
'last_login'
)
read_only_field = ('start_date',)
class RegisterSerializer(UserSerializer):
password = serializers.CharField(max_length=128, min_length=8, required=True, write_only=True)
email = serializers.EmailField(max_length=128, required=True, write_only=True)
username = serializers.CharField(max_length=128, required=True)
name = serializers.CharField(max_length=128, required=True)
class Meta:
model = Account
fields = [
'email',
'username',
'password',
'name',
]
extra_kwargs = {'password': {'wirte_only': True}}
def create(self, validated_data):
password = validated_data.pop('password', None)
instance = self.Meta.model(**validated_data)
if password is not None:
instance.set_password(password)
instance.save()
return instance
class LoginSerialier(TokenObtainPairSerializer):
def validate(self, attrs):
data = super().validate(attrs)
refresh = self.get_token(self.user)
data['user'] = UserSerializer(self.user).data
data['refresh'] = str(refresh)
data['access'] = str(refresh.access_token)
if api_settings.UPDATE_LAST_LOGIN:
update_last_login(None, self.user)
return data
自定义用户serializer.py:
class RegisterViewSet(viewsets.ModelViewSet):
permission_classes = (AllowAny,)
def create(self, request, format='json'):
serializer = RegisterSerializer(data=request.data)
if serializer.is_valid():
user = serializer.save()
if user:
json = serializer.data
return Response(json, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class LoginViewSet(viewsets.ModelViewSet, TokenObtainPairView):
serializer_class = LoginSerialier
permission_classes = (AllowAny,)
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
try:
serializer.is_valid(raise_exception=True)
except TokenError as e:
raise InvalidToken(e.args[0])
return Response(serializer.validated_data, status=status.HTTP_200_OK)
class RefreshViewSet(viewsets.ViewSet, TokenRefreshView):
permission_classes = (AllowAny,)
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
try:
serializer.is_valid(raise_exception=True)
except TokenError as e:
raise InvalidToken(e.args[0])
return Response(serializer.validated_data, status=status.HTTP_200_OK)
class UserViewSet(viewsets.ModelViewSet):
queryset = Account.objects.none()
serializer_class = UserSerializer
permission_classes = (IsAuthenticated,)
def get_queryset(self):
return Account.objects.filter(id=self.request.user.id)
class BlacklistTokenView(viewsets.ModelViewSet):
permission_classes = (AllowAny,)
def post(self, request):
try:
refresh_token = request.data["refresh_token"]
token = RefreshToken(refresh_token)
token.blacklist()
return Response(status=status.HTTP_205_RESET_CONTENT)
except Exception as e:
return Response(status=status.HTTP_400_BAD_REQUEST)
线程 serializer.py:
class ThreadSerializer(serializers.ModelSerializer):
created = serializers.DateTimeField(format="%d %B, %Y %H:%M:%S")
class Meta:
model = Thread
fields = (
'id',
'username',
'profile_image',
'alt',
'image',
'content',
'created',
'status')
线程view.py
class ThreadView(generics.ListAPIView):
permission_classes = (IsAuthenticated,)
serializer_class = ThreadSerializer
def get_queryset(self):
username = self.request.user.username
return Thread.objects.filter(username=username)
class ThreadViewSets(viewsets.ModelViewSet):
permission_classes = (AllowAny,)
serializer_class = ThreadSerializer
queryset = Thread.objects.all()
我认为你可以处理上传的图片。
class ThreadSerializer(serializers.ModelSerializer):
created = serializers.DateTimeField(format="%d %B, %Y %H:%M:%S")
uploaded_image = serializers.FileField(
max_length = 1000000,
allow_empty_file = False,
write_only = True
)
profile_image = UserSerializer(read_only = True)
class Meta:
model = Thread
fields = (
...,
'uploaded_image',
)
extra_kwargs = {
'image': { 'read_only': True }
}
def create(self, validated_data):
image_data = validated_data.pop('uploaded_image')
thread_obj = Thread(**validated_data)
thread_obj.image = image_data
thread_obj.save()
return thread_obj
在前端,您需要使用密钥uploaded_image
上传图片。
我现在正尝试将两个 foreignkey
分配给同一个自定义用户模型,即用户名和个人资料图像字段。
自定义用户模型:
class Account(AbstractBaseUser, PermissionsMixin):
class Meta:
verbose_name_plural = "Account List"
email = models.EmailField(max_length=255, unique=True)
username = models.CharField(max_length=255, unique=True)
name = models.CharField(max_length=255, default="")
profile_image = models.ImageField(max_length=255, upload_to=profile_image_path, blank=True, unique=True)
about = models.TextField(max_length=255, default='Write something about yourself...', blank=True)
start_date = models.DateTimeField(default=timezone.now)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
last_login = models.DateTimeField(auto_now=True)
objects = AccountManager()
USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["username", "name"]
def __str__(self):
return self.username
线程模型:
class Thread(models.Model):
options = (('active', 'Active'), ('deactivated', 'Deactivated'))
username = models.ForeignKey(Account, on_delete=models.CASCADE, to_field='username', related_name='user')
profile_image = models.ForeignKey(Account, on_delete=models.CASCADE, to_field='profile_image', related_name='profile')
alt = models.TextField(max_length=255, blank=True)
image = models.ImageField(max_length=255, upload_to=thread_image_path, default='images/family.png')
content = models.TextField(blank=True)
created = models.DateTimeField(default=timezone.now)
status = models.CharField(max_length=11, choices=options, default='active')
但是,每当我尝试创建线程时,我总是遇到错误:
UnicodeDecodeError at /api/public/thread/
'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
Request Method: GET
Request URL: http://127.0.0.1:8000/api/public/thread/
Django Version: 4.0.4
Exception Type: UnicodeDecodeError
Exception Value:
'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
Exception Location: c:\Users291\Desktop\vscode\my-app\web\env\lib\site-packages\rest_framework\utils\encoders.py, line 50, in default
这个错误发生在我将外键分配给配置文件图像之后,序列化程序无法将图像序列化到 API 端点。我试过 encoders.py
并将编码器更改为 utf16,但问题仍然存在。
有人知道我的代码有什么问题吗?
编辑:
自定义用户serializer.py:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = (
'id',
'email',
'username',
'name',
'profile_image',
'about',
'start_date',
'is_active',
'last_login'
)
read_only_field = ('start_date',)
class RegisterSerializer(UserSerializer):
password = serializers.CharField(max_length=128, min_length=8, required=True, write_only=True)
email = serializers.EmailField(max_length=128, required=True, write_only=True)
username = serializers.CharField(max_length=128, required=True)
name = serializers.CharField(max_length=128, required=True)
class Meta:
model = Account
fields = [
'email',
'username',
'password',
'name',
]
extra_kwargs = {'password': {'wirte_only': True}}
def create(self, validated_data):
password = validated_data.pop('password', None)
instance = self.Meta.model(**validated_data)
if password is not None:
instance.set_password(password)
instance.save()
return instance
class LoginSerialier(TokenObtainPairSerializer):
def validate(self, attrs):
data = super().validate(attrs)
refresh = self.get_token(self.user)
data['user'] = UserSerializer(self.user).data
data['refresh'] = str(refresh)
data['access'] = str(refresh.access_token)
if api_settings.UPDATE_LAST_LOGIN:
update_last_login(None, self.user)
return data
自定义用户serializer.py:
class RegisterViewSet(viewsets.ModelViewSet):
permission_classes = (AllowAny,)
def create(self, request, format='json'):
serializer = RegisterSerializer(data=request.data)
if serializer.is_valid():
user = serializer.save()
if user:
json = serializer.data
return Response(json, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class LoginViewSet(viewsets.ModelViewSet, TokenObtainPairView):
serializer_class = LoginSerialier
permission_classes = (AllowAny,)
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
try:
serializer.is_valid(raise_exception=True)
except TokenError as e:
raise InvalidToken(e.args[0])
return Response(serializer.validated_data, status=status.HTTP_200_OK)
class RefreshViewSet(viewsets.ViewSet, TokenRefreshView):
permission_classes = (AllowAny,)
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
try:
serializer.is_valid(raise_exception=True)
except TokenError as e:
raise InvalidToken(e.args[0])
return Response(serializer.validated_data, status=status.HTTP_200_OK)
class UserViewSet(viewsets.ModelViewSet):
queryset = Account.objects.none()
serializer_class = UserSerializer
permission_classes = (IsAuthenticated,)
def get_queryset(self):
return Account.objects.filter(id=self.request.user.id)
class BlacklistTokenView(viewsets.ModelViewSet):
permission_classes = (AllowAny,)
def post(self, request):
try:
refresh_token = request.data["refresh_token"]
token = RefreshToken(refresh_token)
token.blacklist()
return Response(status=status.HTTP_205_RESET_CONTENT)
except Exception as e:
return Response(status=status.HTTP_400_BAD_REQUEST)
线程 serializer.py:
class ThreadSerializer(serializers.ModelSerializer):
created = serializers.DateTimeField(format="%d %B, %Y %H:%M:%S")
class Meta:
model = Thread
fields = (
'id',
'username',
'profile_image',
'alt',
'image',
'content',
'created',
'status')
线程view.py
class ThreadView(generics.ListAPIView):
permission_classes = (IsAuthenticated,)
serializer_class = ThreadSerializer
def get_queryset(self):
username = self.request.user.username
return Thread.objects.filter(username=username)
class ThreadViewSets(viewsets.ModelViewSet):
permission_classes = (AllowAny,)
serializer_class = ThreadSerializer
queryset = Thread.objects.all()
我认为你可以处理上传的图片。
class ThreadSerializer(serializers.ModelSerializer):
created = serializers.DateTimeField(format="%d %B, %Y %H:%M:%S")
uploaded_image = serializers.FileField(
max_length = 1000000,
allow_empty_file = False,
write_only = True
)
profile_image = UserSerializer(read_only = True)
class Meta:
model = Thread
fields = (
...,
'uploaded_image',
)
extra_kwargs = {
'image': { 'read_only': True }
}
def create(self, validated_data):
image_data = validated_data.pop('uploaded_image')
thread_obj = Thread(**validated_data)
thread_obj.image = image_data
thread_obj.save()
return thread_obj
在前端,您需要使用密钥uploaded_image
上传图片。