0

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?

1 Answer 1

1

Just use reverse relation:

Blog.objects.filter(blogauthor__author=request.user)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.