问题描述
我有一个简单的主题,其中包含一个图像字段,一个主题标题,一个主题内容,一个主题标签等。
为图像字段添加了序列化器。
serializers.py
class TopicDetailSerializer(serializers.ModelSerializer):
topic_author = serializers.SerializerMethodField('get_topic_author')
topic_author_picture = serializers.SerializerMethodField(
'get_topic_author_picture')
class Meta:
model = Topic
fields = ['id','topic_title','topic_content','created_date','topic_slug','thread_title','topic_author','topic_author_picture',]
def get_topic_author_picture(self,topic):
return topic.owner.profile_picture
def get_topic_author(self,topic):
return topic.owner.username
当我从前端请求数据时,控制台中的输出:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
我不只是存储图像的路径,而不是图像本身吗?我的意思是我有一个用户配置文件序列化程序,该序列化程序将信息发送给所请求的用户,并且其中包含一个图像。但是效果很好。
解决方法
使用FieldFile.url
作为
def get_topic_author_picture(self,topic):
return topic.owner.profile_picture.url