Answer the question
In order to leave comments, you need to log in
How to properly filter Django Queryset and apply annotate?
I want to filter chats with the participation of the user who requests these chats and count the number of participants for each chat
class Chat(models.Model):
users = models.ManyToManyField(User, related_name='chats')
def get_queryset(self):
return Chat.objects.annotate(user_count=Count('users')).filter(users__id=self.request.user.id)
def get_queryset(self):
return Chat.objects.filter(users__id=self.request.user.id).annotate(user_count=Count('users'))
{
"id": 146,
"user_count": 2
},
{
"id": 109,
"user_count": 3
},
{
"id": 22,
"user_count": 11
}
{
"id": 146,
"user_count": 1
},
{
"id": 109,
"user_count": 1
},
{
"id": 22,
"user_count": 1
}
Answer the question
In order to leave comments, you need to log in
an error in the second because at first you cut off all other users (except one) with a filter and count the number of users for it, the
difference in speed will be if the number of participants is large.
try like this:
def get_queryset(self):
qs = Chat.objects.filter(users__id=self.request.user.id).values_list('id')
return Chat.objects.filter(id__in=qs).annotate(user_count=Count('users'))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question