Reputation: 365
I have these models (names are changed):
class Blog(models.Model):
created_dtm = models.DateTimeField()
updated_dtm = models.DateTimeField()
title = models.CharField(max_length=200, null=True, blank=True)
creator = models.ForeignKey(Authors, on_delete=models.CASCADE, null=True, blank=True)
class BlogAuthor(models.Model):
blog = models.ForeignKey(Blog, on_delete=models.CASCADE)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
class Meta:
unique_together = ('blog', 'author')
and I need to get all Blogs(and its details) in which author is involved. The problem is that each Blog can have more than one author.
Now I can get query set of Blog's ids using this:
BlogAuthor.objects.filter(author=request.user).values_list('blog_id', flat=True)
and then try getting every single Blog one by one using cycle for
, but is it possible to get list of Blogs with one query?
Upvotes: 0
Views: 119
Reputation: 47374
Just use reverse relation:
Blog.objects.filter(blogauthor__author=request.user)
Upvotes: 1