【发布时间】:2016-06-07 16:22:14
【问题描述】:
我有一个我必须呈现的“帖子”列表。对于每个帖子,我必须做三个过滤查询集,将它们组合在一起,然后计算对象的数量。这合理吗?哪些因素可能会导致速度变慢?
这大概是我的代码:
def viewable_posts(request, post):
private_posts = post.replies.filter(permissions=Post.PRIVATE, author_profile=request.user.user_profile).order_by('-modified_date')
community_posts = post.replies.filter(permissions=Post.COMMUNITY, author_profile__in=request.user.user_profile.following.all()).order_by('-modified_date')
public_posts = post.replies.filter(permissions=Post.PUBLIC).order_by('-modified_date')
mixed_posts = private_posts | community_posts | public_posts
return mixed_posts
def viewable_posts_count(request, post):
return viewable_posts(request, post).count()
【问题讨论】:
-
请发布一些代码:模型,查询。 “慢”是索引、到数据库的往返、需要从数据库到 Web 服务器的数据量等问题。非常具体
-
呃,我粗略地添加了视图代码。我认为该模型非常明显。
标签: django database django-models django-orm