Django Rest Framework - How can I get the unlimited depth of a specific field in a serializer

I have posts and replies to posts. Replies can have parents unless the reply is the top reply. Like twitter you can reply to a reply and if you click on a reply you can see the reply and the parents of the reply so there is a hierarchy of replies.

I have two questions please.

ONE:

How can I display the hierarchy in an endpoint so it goes as far as the rabbit hole goes? At the moment I only see the reply and then the parent of that reply. I cannot see the parent of the parent of the reply. It stops on the first level upwards?

I don't want to make the depth setting to be more than 1, so looking for an alternative way or maybe the better or correct way.

Here is my post serializer:

class PostSerializer(serializers.ModelSerializer):
    images = PostImageSerializer(many=True, read_only=True, required=False)
    profile = serializers.SerializerMethodField()
    replies = PostRepliesSerializer(many=True, read_only=True, required=False)

    class Meta:
        model = Post
        fields = [
            "id",
            "can_view",
            "can_comment",
            "category",
            "body",
            "images",
            "video",
            "profile",
            "published",
            "created_at",
            "updated_at",
            "replies",

        ]
        depth = 1

    def get_profile(self, obj):
        profile_obj = Profile.objects.get(id=obj.user.profile.id)
        profile = ShortProfileSerializer(profile_obj)
        return profile.data
 
    def create(self, validated_data):
        user = User.objects.get(id=self.context['request'].data.get('user'))
        category = Category.objects.get(id=self.context['request'].data.get('category'))
        new_post = Post.objects.create(**validated_data, category=category, user=user)
        images = dict((self.context['request'].FILES).lists()).get('images', None)
        if images:
            for image in images:
                PostImage.objects.create(
                    image=image, post=new_post
                )
        return new_post

and here is my reply serializer:

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

    class Meta:
        model = Reply
        fields = [
            "id",
            "body",
            "parent",
            "video",
            "replies",
            "created_at",
            "updated_at",
            "images",
            "post",
            "post_images",
            "profile",
            
        ]
        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

TWO:

My second question is maybe a bit unrelated and just something that bugs me about Python. If I declare a serializer (let's call it ONE) in the file and I want to change a field in another serializer (TWO) then I cannot use the ONE serializer if its code is below TWO, yet I am able to reference ONE in a serializer TWO method and it's allowed. Why can I not reference or use a serializer that is further down the page? I know that is how Python works but just wondering if there is a way past it? Do I have to then create a copy and put it above the code that references it?

Back to Top