Nikita Alexander
Nikita Alexander

Reputation: 567

Django Rest Framework - Serializer doesn't work

I am trying to create a blog API that would consist of user posts and comment instances. I am using Django & Django Rest Framework to build the API that would return data in JSON format. Below is an example of the JSON data being 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)
            ]
        }
    ]
}

My problem is that while the Post images are returned in the JSON correctly, my PostComment images are returned just as an array of id, without the URLs. I indicated with arrows in the code where I'm trying to get an array of objects (id & URLs). I suspect this would be happening due to some issues in the serializer, but I just can't pinpoint it. Here is my serializer code:

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

As you can see in my PostCommentSerializer I added the linecomment_images = PostCommentImageSerializer(), so it should be returning an array of objects, not just an array of id integers, and the related_name in models.py is set correctly to 'comment_images', so I am at a loss of what could be the source of the problem.

I suspect it must be something simple, but I can't find the reason behind it. Any help would be hugely appreciated!

Upvotes: 0

Views: 1349

Answers (1)

Subhash Tulsyan
Subhash Tulsyan

Reputation: 146

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')

Upvotes: 1

Related Questions