【问题标题】:How do I display related fields content that is part of the children of a post如何显示属于帖子子项的相关字段内容
【发布时间】:2023-01-07 21:48:51
【问题描述】:

我有这个模型,它是对帖子的回复。但回复也可以有回复,它可以永远进行下去。如果我查看回复,我想查看做出回复的用户个人资料,这是另一个回复的子项?

这是回复模型

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]

这是回复的序列化程序,我可以获得父序列化程序所需的信息,但如何提取子序列化程序的数据?例如,回复有一个作者,而该作者有一个个人资料。我可以拉入父回复的配置文件,但如何拉取子回复的配置文件?

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

返回的数据看起来像这样,在回复下有一个用户 ID,我想为该用户提取一个配置文件?

{"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":"cal@calvincani.com","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"}

【问题讨论】:

    标签: django django-rest-framework


    【解决方案1】:

    嗨,给我你的电子邮件,我找到了你问题的答案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-20
      • 2018-01-10
      • 1970-01-01
      相关资源
      最近更新 更多