I have models.py like this:
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)
How do I get the all the related objects if given the author id. eg. In the 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)
The problem of the function above will causes extra query for each of the post. Is there any way to call all the objects in single query??