[英]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.