I am trying to get all data related to model Post using one single API call. This call should include post contents, comments, upvote and downvotes.
I have 4 models and Post model is a foreign key to every other model.
class Post(models.Model):
title = models.CharField(max_length=200, null=True, blank=True)
body = models.CharField(max_length=500, null=True, blank=True)
def __str__(self):
return str(self.title)
class UpVote(models.Model):
post = models.OneToOneField(Post, related_name="upvote_post", on_delete=models.CASCADE)
upvotes = models.IntegerField(default=0)
class DownVote(models.Model):
post = models.OneToOneField(Post, related_name="downvote_post", on_delete=models.CASCADE)
downvotes = models.IntegerField(default=0)
class Comment(models.Model):
post = models.ForeignKey(Post, related_name="comment_post", on_delete=models.CASCADE)
comment = models.CharField(max_length=200, null=True, blank=True)
I saw this solution in a post to combine data with a foreign key relationship that exists.
class UpVoteSerializer(serializers.ModelSerializer):
class Meta:
model = UpVote
fields = "__all__"
class DownVoteSerializer(serializers.ModelSerializer):
class Meta:
model = DownVote
fields = "__all__"
class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Comment
fields = "__all__"
class PostSerializer(serializers.ModelSerializer):
upvote = UpVoteSerializer(read_only=True, many=False)
downvote = DownVoteSerializer(read_only=True, many=False)
comments = CommentSerializer(read_only=True, many=True)
class Meta:
model = Post
fields = ("id", "title", "body", "upvote", "downvote", "comments")
This is from the views.py file
@api_view(['GET'])
def postList(request):
post = Post.objects.all().order_by('-id')
serializer = PostSerializer(post, many=True)
return Response(serializer.data)
This is the output that get returned
[
{
"id": 7,
"title": "1st post",
"body": "Body of 1st post"
},
{
"id": 6,
"title": "2nd post",
"body": "Body of 2nd post"
},
{
"id": 5,
"title": "3rd post",
"body": "Body of 3rd post"
},
{
"id": 4,
"title": "4th post",
"body": "Body of 4th post"
}
]
I don't know why it's not returning back the comments and the upvotes and downvotes.