# A Post Model
class Post(models.Model):
title = model.CharField()
user = models.ForeignKey(User)
privacy = models.BooleanField()
# query all post that have privacy = False, from a set of friends of type User.
# friends = [user1, user2, user5, user9]
def get_friend_posts(request):
# Repeat the statement below for the size of friends. (4 here)
friend_posts = Post.objects.filter(privacy=False).filter(user=fiends[0])
variables = RequestContext(request, {
'friend_posts' : friend_posts
})
return render_to_response('friends_page.html', variables)
- 什么是最好的和最佳的方式来做这样的数据库查询?
- 另外,如何追加query_set数据类型?在我的例子中,我需要在循环中运行 查询。 (我目前不在代码做)
编辑/ UPDATE:在django中链接和过滤数据库查询的最佳方式是什么?
posts_query = Posts.objects.filter(privacy=False)
for friend in friends:
q_list.append(bmks_query.filter(user=friend))
for q in q_list:
friend_posts += list(chain(q))
有没有更好的办法那么这对我的需要?
#2,我认为我可以使用“连锁”形式“itertools”添加的4个朋友的查询集。 – amehta