Home > Net >  Combining annotation and filtering in Django for two different classes
Combining annotation and filtering in Django for two different classes

Time:11-14

Hi I am trying to query and count marketplaces for every infringement only for the logged in user.

Essentially trying to combine these two.

mar_count = Marketplace.objects.annotate(infringement_count=Count('infringement'))

inf=Infringement.objects.filter(groups__user=request.user)

I found a below example but this is for the same class. I have two separate classes. I am a beginner.

swallow.objects.filter( coconuts_carried__husk__color="green" ).annotate( num_coconuts=Count('coconuts_carried') ).order_by('num_coconuts')

CodePudding user response:

Aggregation functions can take a filter as named parameter

mar_count = Marketplace.objects.annotate(infringement_count=Count('infringement', filter=Q(groups__user=request.user)))
  • Related