One-To-Many Model Serializer not displaying object in django rest framework

I have a one to many model in django rest framework. Video is the parent and Tags are the child, I'm trying to display all the tags in the Video serializer.

class Video(Base):
    video = models.FileField(null=True, blank=True)
    thumbnail = models.ImageField(null=True, blank=True)

class Tag(Base):
    video = models.ForeignKey(Video, on_delete=models.CASCADE, related_name='tags')
    text = models.CharField(max_length=100, null=True, blank=True)
    score = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)

In my serializer I have this,

class VideoSerializer(serializers.ModelSerializer):
    video = serializers.FileField(max_length=None, use_url=True, allow_null=True, required=False)
    thumbnail = serializers.ImageField(max_length=None, use_url=True, allow_null=True, required=False)

    class Meta:
        model = Video
        fields = ('id', 'video', 'thumbnail', 'tags')

The problem is that the serialized data only show the id for the Tags. Any help appreciated.


Solution 1:

You have to add serializer for tag.

For example:

class TagSerializer(Base):
   class Meta:
        model = Tag
        fields = ('id', 'text', 'score')

Change VideoSerializer into something like this:

class VideoSerializer(serializers.ModelSerializer):
    video = serializers.FileField(max_length=None, use_url=True, allow_null=True, required=False)
    thumbnail = serializers.ImageField(max_length=None, use_url=True, allow_null=True, required=False)
    tags = TagSerializer(many=True)

    class Meta:
        model = Video
        fields = ('id', 'video', 'thumbnail', 'tags')