0





16

Advertisement

Through a Django queryset call, I'm generating a list of integer tuples:

[(0,1),(0,2),(0,4),(5,8),.......]

I instead need to generate a list which sums the two elements inside the tuple together, i.e.

[1,2,4,13,.....]

How can I accomplish that within the Django queryset query?

The query I've written so far is:

photos_score_list = Photo.objects.filter(upload_time__gte=yesterday).annotate(unique_comments=Count('photocomment__submitted_by', distinct=True)).values_list('vote_score','unique_comments')
Question author Hassan-baig | Source

Advertisement


0


I don't know about Django, but when you have this:

l = [(0,1),(0,2),(0,4),(5,8)]

you can compute the sum of each element in one line using list comprehension:

l2 = [sum(x) for x in l]

result:

[1, 2, 4, 13]

applied to your problem:

photos_score_sum = [sum(x) for x in Photo.objects.filter(upload_time__gte=yesterday).annotate(unique_comments=Count('photocomment__submitted_by', distinct=True)).values_list('vote_score','unique_comments')]
Answer author Jean-fran%c3%a7ois-fabre