I have two models like this:
class Item(models.Model):
title = models.CharField(max_length=128)
class Order(models.Model):
item = models.ForeignKey(Item)
user = models.ForeignKey('users.User',null=True)
gift_code = models.CharField(max_length=20,default='')
I need to annotate two different query:
Order.objects.filter(gift_code__isnull=False, gift_code__gt='').values('item__title').annotate(the_count=Count('item__title'))
Order.objects.filter(gift_code__isnull=False, gift_code__gt='', user__isnull=False).values('item__title').annotate(the_count=Count('item__title'))
the problem is I can't find a way to merge this two query.
I tried this method, but both value was the same:
all_gift = Count('item__title')
used_gift = Count('item__title', filter=Q(user__isnull=False))
gifts = Order.objects.filter(gift_code__isnull=False, gift_code__gt='').values('item__title').annotate(all_gift=all_gift).annotate(used_gift=used_gift)
the actual output:
[{'item__title': 'title', 'used_gift': 500, 'all_gift': 500},...]
my expected output is:
[{'item__title': 'title', 'used_gift': 60, 'all_gift': 500},...]