现在我的API响应如下所示
"results": [
{
"id": 12,
"blog_slug": "rest-api-django",
"comment": "Parent Comment",
"main_comment_id": null
},
{
"id": 13,
"blog_slug": "rest-api-django",
"comment": "Child Comment of 12",
"main_comment_id": 12
},
{
"id": 14,
"blog_slug": "rest-api-django",
"name": "Mr Ahmed",
"comment": "Child Comment OF 13",
"comment_uuid": "c0b389bc-3d4a-4bda-b6c6-8d3ef494c93c",
"main_comment_id": 13
}
]
字符串
我想要这样的东西
{
"results": [
{
"id": 12,
"blog_slug": "rest-api-django",
"comment": "Parent Comment",
"main_comment_id": null,
"children": [
{
"id": 13,
"blog_slug": "rest-api-django",
"comment": "Child Comment of 12",
"main_comment_id": 12,
"children": [
{
"id": 14,
"blog_slug": "rest-api-django",
"name": "Mr Ahmed",
"comment": "Child Comment OF 13",
"comment_uuid": "c0b389bc-3d4a-4bda-b6c6-8d3ef494c93c",
"main_comment_id": 13
}
]
}
]
}
]
}
型
所以每个孩子都在父母之下,这是我模式
class BlogComment(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,blank=True,null=True)
blog_slug = models.CharField(max_length=500)
main_comment_id = models.ForeignKey("self",on_delete=models.CASCADE,blank=True,null=True)
name = models.CharField(max_length=250)
email = models.CharField(max_length=250)
comment = models.TextField()
is_published = models.BooleanField(True)
comment_uuid = models.CharField(max_length=250)
型
我的博客评论Serializers
class BlogCommentSerializers(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
comment_uuid = serializers.CharField(read_only=True)
blog_slug = serializers.CharField(max_length=500)
main_comment_id = serializers.CharField(required=False)
name = serializers.CharField(max_length=250)
email = serializers.CharField(max_length=250)
comment = serializers.CharField(style={'base_template': 'textarea.html'},allow_blank=False,required=True)
is_published = serializers.BooleanField(default=True)
def to_representation(self, instance):
data = {
'id': instance.id,
'blog_slug': instance.blog_slug,
'name': instance.name,
'email': instance.email,
'comment': instance.comment,
"comment_uuid": instance.comment_uuid,
"main_comment_id": instance.main_comment_id
}
if instance.main_comment_id:
data['main_comment_id'] = instance.main_comment_id.id # Change to the ID number
return data
型
下面是我API函数
if request.method == 'GET':
paginator = PageNumberPagination()
paginator.page_size = 1
blogs = Blog.objects.filter(is_approved=True).all()
result_page = paginator.paginate_queryset(blogs, request)
serializer = BlogSerialzers(result_page, many=True)
paginated_response = paginator.get_paginated_response(serializer.data)
if paginated_response.get('next'):
paginated_response.data['next'] = paginated_response.get('next')
if paginated_response.get('previous'):
paginated_response.data['previous'] = paginated_response.get('previous')
return paginated_response
型
2条答案
按热度按时间twh00eeo1#
设置关系的方式,可以使用
SerializerMethodField
:字符串
mhd8tkvw2#
我使用这种类型的结构来嵌套注解。过滤列表API的
parent__isnull=True
很重要,否则,它将在响应中重复。models.py
字符串
serializers.py
型
views.py
型
这里需要注意的一点是,序列化器将被递归地调用以处理所有嵌套的注解。