Reputation: 1031
I know there are many similar questions but none of them solves my problem.
I have a simple topic that has an image field, a topic title, a topic content, a topic slug and etc.
That topic is associated with a user using the foreignkey. The serializer is doing fine until the
serializer for the image field was added.
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
The output in the console when I request the data from frontend:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
Ain't I just storing the path to the Image rather than the image itself? I mean I have a user profile serializer which sends information to the requested user and it includes an image. But it works fine.
Upvotes: 0
Views: 1568
Reputation: 88669
use FieldFile.url
as
def get_topic_author_picture(self, topic):
return topic.owner.profile_picture.url
Upvotes: 1