Queryset with annotations gave me result list like this:
[{'completed__sum': 1, 'offer__count': 2, 'offer': 1}, {'completed__sum': 0, 'offer__count': 1, 'offer': 2}]
I want to get something like that:
{1:{'completed__sum': 1, 'offer__count': 2},2:{'completed__sum': 0, 'offer__count': 1}}
where keys 1 and 2 are values of 'offer': X field.
What is the fastest way to convert result of query to needed form? Or is there any way to receive result of queryset in that format?
Here is my QuerySet:
Progress.objects.filter(user=self.request.user).values('offer').annotate(Count('offer')).annotate(Sum('completed'))
My model:
class Progress(models.Model):
user = models.ForeignKey(to=User)
offer = models.ForeignKey(to=Offer)
completed = models.BooleanField(default=False)
works on python 2.6 +
use the dict constructor with enumerate.
edit: just found a much better way to do this. if you arent using the list anymore and are going to keep only the final results.