【发布时间】:2014-07-22 02:57:31
【问题描述】:
我有这样的models.py:
class Post(models.Model):
name = models.CharField(max_length=100,blank=True)
author = models.ForeignKey(User, null=True, blank=True)
...
class Categories(models.Model):
category = models.CharField(max_length=200)
post_id = models.ForeignKey(Post)
class Tags(models.Model):
tag = = models.CharField(max_length=200)
tag_id = models.ForeignKey(Post)
如果给定作者 ID,我如何获取所有相关对象。 例如。在view.py中
def AllPost(request, userid):
posts = Post.objects.filter(author=userid)
for post in Posts:
category = Categories.filter(post_id = post.id)
tag = Tags.filter(post_id = post.id)
上述函数的问题会导致对每个帖子进行额外的查询。有没有办法在单个查询中调用所有对象??
【问题讨论】: