Home > Software engineering >  How do I display related fields content that is part of the children of a post
How do I display related fields content that is part of the children of a post

Time:01-08

I have this model that is a reply to a post. But replies can also have replies and it can carry on forever. If I look at a reply I would like to see the user profile that made the reply. I can see the profile of the user that made the parent reply but not the profile of the user that made the child reply.

Here is the reply model

class Reply(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    post = models.ForeignKey(Post, on_delete=models.CASCADE)
    video = models.FileField(
        upload_to=reply_videos_directory_path, null=True, blank=True
    )
    body = models.TextField(max_length=256, default=None)
    parent = models.ForeignKey(
        "self", on_delete=models.CASCADE, null=True, blank=True, related_name="replies"
    )

    created_at = models.DateTimeField(auto_now_add=True, verbose_name="Created at")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="Updated at")

    class Meta:
        verbose_name = "post reply"
        verbose_name_plural = "post replies"
        db_table = "post_reply"

    def __str__(self):
        return self.body[0:30]

and here is the serializer for the reply and I can get the information I need for the parent reply but how do I pull in the profile data of a child reply? For example replies have an author and that author has a profile. I can pull in the profile for the parent reply but how do I pull the profile in for the child reply?

class ReplySerializer(serializers.ModelSerializer):
    images = ReplyImageSerializer(many=True, read_only=True, required=False)
    post = serializers.SerializerMethodField()
    profile = serializers.SerializerMethodField()
    post_images = serializers.SerializerMethodField()

    class Meta:
        model = Reply
        fields = [
            "id",
            "post",
            "post_images",
            "video",
            "images",
            "body",
            "parent",
            "replies",
            "profile",
            "created_at",
            "updated_at",
        ]
        depth = 1

    def get_post(self, obj):
        post_obj = Post.objects.get(id=obj.post.id)
        post = ShortPostSerializer(post_obj)
        return post.data

    def get_profile(self, obj):
        profile_obj = Profile.objects.get(id=obj.user.profile.id)
        profile = ShortProfileSerializer(profile_obj)
        return profile.data

    def get_post_images(self, obj):
        images = PostImage.objects.filter(post=obj.post.id)
        serializer = PostImageSerializer(images, many=True)
        return serializer.data     
    
    def create(self, validated_data):
        new_reply = Reply.objects.create(**validated_data)
        images = dict((self.context['request'].FILES).lists()).get('images', None)
        if images:
            for image in images:
                ReplyImage.objects.create(
                    image=image, reply=new_reply
                )
        return new_reply

The data returned looks like this and under replies there is a user id and I would like to pull in the profile for that user?

{"id":8,"post":{"body":"Post TWO by the user koos","category":{"id":1,"name":"General","slug":"general","description":"General post category","created_at":"2022-12-06T01:49:34.683671Z","updated_at":"2022-12-06T01:49:34.683693Z","parent":null},"video":null,"created_at":"2023-01-05T04:40:02.994033Z"},"post_images":[{"id":47,"image":"/storage/posts/52/images/bg3.jpg","post":52},{"id":48,"image":"/storage/posts/52/images/bg4.jpg","post":52},{"id":49,"image":"/storage/posts/52/images/bg5.jpg","post":52},{"id":50,"image":"/storage/posts/52/images/bg7.jpg","post":52}],"video":null,"images":[{"id":7,"image":"http://127.0.0.1:8000/storage/replies/8/images/bmw-rr-3.jpg","reply":8}],"body":"Comment 1 on post 2 by user koos","parent":null,"replies":[{"id":6,"video":null,"body":"comment 1 on comment 1 on post 2 by user koos","created_at":"2023-01-05T04:45:06.846403Z","updated_at":"2023-01-07T09:23:54.986186Z","user":1,"post":52,"parent":8},{"id":21,"video":"http://127.0.0.1:8000/storage/1/posts/52/replies/8/videos/WhatsApp_Video_2020-10-09_at_10.22.36.mp4","body":"Comment 2 on comment 1 of post 2 by user koos","created_at":"2023-01-07T10:46:14.134659Z","updated_at":"2023-01-07T10:46:14.134696Z","user":1,"post":52,"parent":8}],"profile":{"photo":"/storage/profiles/images/1/tumblr_88af0e2145b389cc684a7c343fa9980a_d96ebf87_1280_p2cSfII.jpg","first_name":"Calvin","middle_name":"","last_name":"Cani","user":{"id":1,"username":"koos","role":"EM","email":"[email protected]","is_staff":true,"is_superuser":true,"is_advertiser":false}},"created_at":"2023-01-05T04:47:30.017931Z","updated_at":"2023-01-07T09:17:58.476112Z"}

Also if I pull in the profile for the user in the reply serializer in any case why am I not seeing it for the child replies?

I see that the fields in the child replies match that of the model. so what serializes those fields?

Is there another way to approach this and should I do it differently? This seems to be quite hard?

CodePudding user response:

I'm not sure this would work but have you tried declaring the field with its own serializer?

replies = ReplySerializer(many=True, read_only=True, required=False)
  • Related