How to add fields to serializer

Опубликовано: 26 Май 2026
на канале: Emrah KAYA
4
1

Hello everyone! I hope this video has helped solve your questions and issues. This video is shared because a solution has been found for the question/problem. I create videos for questions that have solutions. If you have any other issues, feel free to reach out to me on Instagram:   / ky.emrah  

Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!How to add fields to serializer

I would like to add foreign key fields to DRF serializer. I need API endpoint with information of Publication details with all Comments to this publication with all Images to this publication and with Likes number to this publication.
Models
class Publication(models.Model):
pub_text = models.TextField(null=True, blank=True)
pub_date = models.DateTimeField(auto_now_add=True)
pub_author = models.ForeignKey(User, on_delete=models.CASCADE)


class Comment(models.Model):
com_text = models.TextField(null=True, blank=True)
com_like = models.BooleanField(default=False)
com_author = models.ForeignKey(User, on_delete=models.CASCADE)
com_to_pub = models.ForeignKey(Publication, on_delete=models.CASCADE, related_name='comment_author')
com_date = models.DateTimeField(auto_now_add=True, null=True)


class Image(models.Model):
image = models.ImageField(upload_to='images', null=True)
image_to_pub = models.ForeignKey(Publication, on_delete=models.CASCADE, null=True, related_name='images')

class Publication(models.Model):
pub_text = models.TextField(null=True, blank=True)
pub_date = models.DateTimeField(auto_now_add=True)
pub_author = models.ForeignKey(User, on_delete=models.CASCADE)


class Comment(models.Model):
com_text = models.TextField(null=True, blank=True)
com_like = models.BooleanField(default=False)
com_author = models.ForeignKey(User, on_delete=models.CASCADE)
com_to_pub = models.ForeignKey(Publication, on_delete=models.CASCADE, related_name='comment_author')
com_date = models.DateTimeField(auto_now_add=True, null=True)


class Image(models.Model):
image = models.ImageField(upload_to='images', null=True)
image_to_pub = models.ForeignKey(Publication, on_delete=models.CASCADE, null=True, related_name='images')

Serializers
class ImageSerializer(serializers.ModelSerializer):
class Meta:
model = Image
fields = ['image']


class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Comment
fields = ['com_text', 'com_like', 'com_date', 'com_author']


class PublicationSerializer(serializers.ModelSerializer):
class Meta:
model = Publication
fields = ['id', 'pub_text', 'pub_author', 'pub_date']

def to_representation(self, instance):
representation = super().to_representation(instance)
representation['comment'] = CommentSerializer(instance.comment_author.all(), many=True).data
representation['image'] = ImageSerializer(instance.images.all(), many=True).data
return representation

class ImageSerializer(serializers.ModelSerializer):
class Meta:
model = Image
fields = ['image']


class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Comment
fields = ['com_text', 'com_like', 'com_date', 'com_author']


class PublicationSerializer(serializers.ModelSerializer):
class Meta:
model = Publication
fields = ['id', 'pub_text', 'pub_author', 'pub_date']

def to_representation(self, instance):
representation = super().to_representation(instance)
representation['comment'] = CommentSerializer(instance.comment_author.all(), many=True).data
representation['image'] = ImageSerializer(instance.images.all(), many=True).data
return representation

Views
class PublicationViewSet(viewsets.ModelViewSet):
queryset = Publication.objects.all()Source of the question:
https://stackoverflow.com/questions/7...

Question and source license information:
https://meta.stackexchange.com/help/l...
https://stackoverflow.com/