I want my comment serializer response to be like this -
[
{
"id": 50,
"comment": "50Reply1",
"timestamp": "2021-05-20 00:26:41",
"user": {
"hnid": "d0a04c7b-6399-44db-ba7c-4ef39ae7e59c",
"username": "Sunasuns #GD6GXAJ4",
"profile_img": "/media/Bay-Morning-Sea-Clouds-Beach-House-Wallpaper-1162x768.jpg",
"full_name": "Suna suns"
},
"reply_comment":
{
"id": 51,
"comment": "50Reply2",
"timestamp": "2021-05-20 00:26:46",
"user": {
"hnid": "d0a04c7b-6399-44db-ba7c-4ef39ae7e59c",
"username": "Sunasuns #GD6GXAJ4",
"profile_img": "/media/Bay-Morning-Sea-Clouds-Beach-House-Wallpaper-1162x768.jpg",
"full_name": "Suna suns"
},
"reply_comment": 50
},
{
"id": 52,
"comment": "50Reply3",
"timestamp": "2021-05-20 00:26:51",
"user": {
"hnid": "d0a04c7b-6399-44db-ba7c-4ef39ae7e59c",
"username": "Sunasuns #GD6GXAJ4",
"profile_img": "/media/Bay-Morning-Sea-Clouds-Beach-House-Wallpaper-1162x768.jpg",
"full_name": "Suna suns"
},
"reply_comment": 50
},
}]
But instead I am getting something like this -
[ {
"id": 52,
"comment": "50Reply2",
"timestamp": "2021-05-20 00:26:46",
"user": {
"hnid": "d0a04c7b-6399-44db-ba7c-4ef39ae7e59c",
"username": "Sunasuns #GD6GXAJ4",
"profile_img": "/media/Bay-Morning-Sea-Clouds-Beach-House-Wallpaper-1162x768.jpg",
"full_name": "Suna suns"
},
"reply_comment": 50
},
{
"id": 51,
"comment": "50Reply1",
"timestamp": "2021-05-20 00:26:41",
"user": {
"hnid": "d0a04c7b-6399-44db-ba7c-4ef39ae7e59c",
"username": "Sunasuns #GD6GXAJ4",
"profile_img": "/media/Bay-Morning-Sea-Clouds-Beach-House-Wallpaper-1162x768.jpg",
"full_name": "Suna suns"
},
"reply_comment": 50
},
{
"id": 50,
"comment": "Reply3",
"timestamp": "2021-05-20 00:24:51",
"user": {
"hnid": "d0a04c7b-6399-44db-ba7c-4ef39ae7e59c",
"username": "Sunasuns #GD6GXAJ4",
"profile_img": "/media/Bay-Morning-Sea-Clouds-Beach-House-Wallpaper-1162x768.jpg",
"full_name": "Suna suns"
},
"reply_comment": null
},]
Here is my model.py
class Comment(models.Model):
post = models.ForeignKey(Posts, related_name='comments', on_delete=models.CASCADE)
user = models.ForeignKey(HNUsers, related_name='comments', on_delete=models.CASCADE)
comment = models.TextField("Comment", blank=True, null=True)
timestamp = models.DateTimeField("Timestamp", blank=True, null=True, auto_now_add=True)
reply_comment = models.ForeignKey('self', related_name='replies', on_delete=models.CASCADE, blank=True, null=True)
class Meta:
verbose_name_plural = "Comments"
My get views.py
if request.method == 'GET':
data = request.data
comments = Comment.objects.filter(post_id=data['post']).order_by('-timestamp')
print(comments)
serializer = CommentGetValueSerializer(comments, many=True)
print("print ", serializer)
return Response(serializer.data, status=status.HTTP_201_CREATED)
My get serializers.py
class CommentUserSerializer(serializers.ModelSerializer):
class Meta:
model = HNUsers
fields = (
'hnid',
'username',
'profile_img',
'full_name',
)
class CommentGetValueSerializer(serializers.ModelSerializer):
user = CommentUserSerializer(read_only=True, many=False)
timestamp = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")
# reply_comment = serializers.SerializerMethodField()
#comments = serializers.SerializerMethodField()
class Meta:
model = Comment
fields = (
'id',
'comment',
'timestamp',
'user',
'reply_comment'
)
Any help regarding this is much appreciated .