【发布时间】:2014-09-11 15:49:57
【问题描述】:
(对不起我的英语)
我正在学习 Python 和 Django。现在,我的挑战是开发线程化的通用评论系统。有两种模型,Post 和 Comment。
-帖子可以评论。
-Comment 可以被评论。 (无尽/线程)
-不应该是系统中的 n+1 查询问题。 (无论多少cmets,都不应该增加查询次数)
我现在的模型是这样的:
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
child = generic.GenericRelation(
'Comment',
content_type_field='parent_content_type',
object_id_field='parent_object_id'
)
class Comment(models.Model):
content = models.TextField()
child = generic.GenericRelation(
'self',
content_type_field='parent_content_type',
object_id_field='parent_object_id'
)
parent_content_type = models.ForeignKey(ContentType)
parent_object_id = models.PositiveIntegerField()
parent = generic.GenericForeignKey(
"parent_content_type", "parent_object_id")
我的模型对吗?以及如何在没有 n+1 查询问题的情况下获得帖子的所有评论(带有层次结构)?
注意:我知道mttp和其他模块,但我想学习这个系统。
编辑:我运行“Post.objects.all().prefetch_related("child").get(pk=1)”命令,这给了我帖子及其子评论。但是当我想获得子命令的子命令时,一个新的查询正在运行。我可以将命令更改为...prefetch_related("child__child__child...")...,然后仍然为每个子父关系深度运行一个新查询。有没有人有解决这个问题的想法?
【问题讨论】:
标签: python django python-2.7 django-models django-views