我有个关于django的博客。我有两篇模型文章和评论。每个注释都可以添加到另一个注释中。如果我发送一个API请求localhost:8000/ comments /1,我将收到带有id="1“和所有嵌套注释到第三层的注释。
api请求后的json localhost:8000/comments/:
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
[
{
"id": 9,
"text": "First comment",
"owner": "alex",
"article": 3,
"parent": null,
"reply_set": [
{
"id": 10,
"text": "Comment to comment",
"owner": "alex",
"article": 3,
"parent": 9,
"reply_set": [
{
"id": 11,
"text": "Third level nested comment",
"owner": "alex",
"article": 3,
"parent": 10
}
]
}
]
},
{
"id": 10,
"text": "Comment to comment",
"owner": "alex",
"article": 3,
"parent": 9,
"reply_set": [
{
"id": 11,
"text": "Third level nested comment",
"owner": "alex",
"article": 3,
"parent": 10,
"reply_set": [
{
"id": 12,
"text": "Forth level nested comment",
"owner": "alex",
"article": 3,
"parent": 11
}
]
}
]
},
{
"id": 11,
"text": "Third level nested comment",
"owner": "alex",
"article": 3,
"parent": 10,
"reply_set": [
{
"id": 12,
"text": "Forth level nested comment",
"owner": "alex",
"article": 3,
"parent": 11,
"reply_set": []
}
]
},
{
"id": 12,
"text": "Forth level nested comment",
"owner": "alex",
"article": 3,
"parent": 11,
"reply_set": []
}
]如何与所有其他嵌套注释一起接收第三级注释?我应该用视图和urls写什么?
评论模式:
class Comment(models.Model):
date_pub = models.DateTimeField(auto_now_add=True)
text = models.TextField(blank=False)
owner = models.ForeignKey('auth.User', related_name='comments', on_delete=models.CASCADE)
article = models.ForeignKey('Article', related_name='comments', on_delete=models.CASCADE)
parent = models.ForeignKey('self', related_name='reply_set', null=True, on_delete=models.PROTECT)
class Meta:
ordering = ['date_pub']注释序列化程序:
class RecursiveSerializer(serializers.Serializer):
def to_representation(self, value):
serializer = self.parent.parent.__class__(value, context=self.context)
return serializer.data
class CommentSerializer(serializers.ModelSerializer):
owner = serializers.ReadOnlyField(source='owner.username')
reply_set = RecursiveSerializer(many=True, read_only=True)
class Meta:
model = Comment
fields = ['id', 'text', 'owner', 'article', 'parent', 'reply_set']评论意见
class CommentList(generics.ListCreateAPIView):
queryset = Comment.objects.all()
serializer_class = serializers.CommentSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
class CommentDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Comment.objects.all()
serializer_class = serializers.CommentSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly,
IsOwnerOrReadOnly]在urlpatterns模式中:
path('comments/', views.CommentList.as_view()),
path('comments/<int:pk>/', views.CommentDetail.as_view())发布于 2022-04-25 18:57:20
如果您的问题是如何接收到特定的第一级评论的所有三级评论(没有他们的父母只是第三级评论),那么您可以像这样过滤查询集
third_level_comment = Comment.objects.filter(replay_set__replay_set__id = <the 1st level comment id>)您使用"__“下面的”双倍“来提升关系链上的一个级别。
https://stackoverflow.com/questions/72002945
复制相似问题