簡體   English   中英

Django Rest 框架 - 序列化器不工作

[英]Django Rest Framework - Serializer doesn't work

我正在嘗試創建一個博客 API,其中包含用戶帖子和評論實例。 我正在使用 Django & Django Rest 框架來構建 API,它將以 JSON 格式返回數據。 以下是返回的 JSON 數據的示例:


    "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)
            ]
        }
    ]
}

我的問題是,雖然在 JSON 中正確返回了Post圖像,但我的PostComment圖像只是作為一個 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() ,因此它應該返回一個對象數組,而不僅僅是一個id整數數組,並且related_name中的models.py被正確設置為“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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM