Django Rest Framework - 序列化程序不起作用

Django Rest Framework - Serializer doesn't work

我正在尝试创建一个包含用户帖子和评论实例的博客 API。我正在使用 Django 和 Django Rest Framework 来构建 API 格式的 return 数据。以下是 JSON 数据被 returned 的示例:


    "id": 2,
    "user": 1,
    "user_photo": "http://127.0.0.1:8000/media/users/abc.jpg",
    "is_owner": true,
    "description": "Hey there, this is my post and I like it",
    "images": [
        {
            "id": 3,
            "post": 2,
            "image": "http://127.0.0.1:8000/media/posts/foo.jpg",
            "comment": "This is image #1"
        },
        {
            "id": 4,
            "post": 2,
            "image": "http://127.0.0.1:8000/media/posts/bar.jpg",
            "comment": "This is image #2"
        }
    ],
    "created": "2022-03-23T16:58:44.800255+03:00",
    "likes": [
        1
    ],
    "comment_count": 1,
    "comments": [
        {
            "id": 3,
            "post": 2,
            "text": "This is a comment on my post",
            "user": 1,
            "likes": [],
            "created": "2022-03-23T17:00:27.074362+03:00",
            "images": [
                3, <----- should be URL to the image, not just id (like above)
                4  <----- should be URL to the image, not just id (like above)
            ]
        }
    ]
}

我的问题是,虽然 Post 图像在 JSON 中正确 returned,但我的 PostComment 图像 returned 就像id 数组,不带 URL。我在代码中用箭头指示了我试图获取对象数组(id 和 URL)的位置。 我怀疑这是由于序列化程序中的某些问题而发生的,但我无法查明它。这是我的序列化程序代码:

class PostCommentImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = PostCommentImage
        comment = serializers.ReadOnlyField(source='comment.id')
        fields = ('id', 'comment', 'image')
        read_only_fields = ('id', 'comment',)

class PostCommentSerializer(serializers.ModelSerializer):
    class Meta:
        model = PostComment
        post = serializers.ReadOnlyField(source='post.id')
        comment_images = PostCommentImageSerializer(many=True, read_only=True)
        fields = ('id', 'post', 'text', 'likes', 'created', 'comment_images')
        read_only_fields = ('id', 'created', 'comment_images', 'likes')

class PostImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = PostImage
        post = serializers.ReadOnlyField(source='post.id')
        fields = ('id', 'post', 'image', 'comment')
        read_only_fields = ('id', 'post')

class PostSerializer(serializers.ModelSerializer):

    user_photo  = serializers.SerializerMethodField(read_only=True)
    is_owner    = serializers.SerializerMethodField(read_only=True)

    images = PostImageSerializer(many=True, read_only=True)
    comments = PostCommentSerializer(many=True, read_only=True)
    comment_count = serializers.ReadOnlyField()

    class Meta:
        model = Post
        post = serializers.ReadOnlyField(source='post.id')
        fields = ('id', 'user', 'user_photo', 'is_owner', 'description', 'images', 'created', 'likes', 'comment_count', 'comments',)
        read_only_fields = ('id', 'user', 'user_photo', 'is_owner', 'images', 'created', 'comments', 'comment_count',)

    def get_user_photo(self, obj):
        request = self.context.get('request')
        user = request.user
        return request.build_absolute_uri(obj.user.photo.url)

    def get_is_owner(self, obj):
        user = self.context['request'].user
        if user == obj.user:
            return True
        else:
            return user.is_superuser

正如你在我的 PostCommentSerializer 中看到的那样,我添加了行 comment_images = PostCommentImageSerializer(),所以它应该是 return 对象数组,而不仅仅是 [=16= 的数组] 整数,并且 models.py 中的 related_name 正确设置为 'comment_images',所以我不知道问题的根源是什么。

我怀疑这一定很简单,但我找不到背后的原因。 任何帮助将不胜感激!

You have defined below serializer field in Meta class of PostCommentSerializer.

comment_images = PostCommentImageSerializer(
            many=True, read_only=True)

Correct Implementation is :

class PostCommentSerializer(serializers.ModelSerializer):
    comment_images = PostCommentImageSerializer(many=True, read_only=True)
class Meta:
    model = PostComment
    post = serializers.ReadOnlyField(source='post.id')
    fields = ('id', 'post', 'text', 'likes', 'created', 'comment_images')
    read_only_fields = ('id', 'created', 'comment_images', 'likes')