[Answered ]-Django postgresql join operation query

2👍

✅

You can use select_related to join the User model in a single query, and annotate
to count the likes for a given post:

from django.db.models import Count

my_id = 42
post = Post.objects.select_related('owner') \
    .annotate(num_likes=Count('likes')) \
    .get(id=my_id)

You can then access the values:

post.id
post.title
post.description
post.owner__name
post.num_likes

Leave a comment