Yasser Mohsen
Yasser Mohsen

Reputation: 1480

Django: get objects.all() with the related rows in another ManyToMany table

I have 3 models:

My question is about how to get all the authors with a field inside each author to show that the authenticated user is following him or not.

I know that I can use the related_name attribute, which returns the UserAuthor manager filtered by specific author, in some way to get it but how?!

My trial is:

class AuthorListView(ListView):
    model = Author

    def get_queryset(self):
        final_list = []
        authors_list = list(Author.objects.all())
        for author in authors_list:
            element = author.userauthors.filter(user=self.request.user)
            final_list.append(element)
        return final_list

I don't like this solution for sure, but how to do it in a better way?

Upvotes: 0

Views: 1546

Answers (1)

user2390182
user2390182

Reputation: 73450

As pointed out by PeterDeGlopper in the comments, you can use annotation:

from django.db.models import Case, When, Value, BooleanField
followed_ids = UserAuthor.objects.\
    filter(user=self.request.user, is_follow=True).\
    values_list('author_id', flat=True)
final_list = Author.objects.annotate(followed=Case(
    When(id__in=followed_ids, then=Value(True)),
    default=Value(False),
    output_field=BooleanField()
))

Now you can access the attribute followed for each instance of this queryset.

Upvotes: 1

Related Questions