Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9072553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:11:46+00:00 2026-06-16T18:11:46+00:00

Queryset with annotations gave me result list like this: [{‘completed__sum’: 1, ‘offer__count’: 2, ‘offer’:

  • 0

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)  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:11:47+00:00Added an answer on June 16, 2026 at 6:11 pm

    works on python 2.6 +

    use the dict constructor with enumerate.

    list_of_dicts = [
        {'completed__sum': 1, 'offer__count': 2, 'offer': 1},
        {'completed__sum': 0, 'offer__count': 1, 'offer': 2}]
    
    dictionary = dict((d['offer'], dict((k, v) for k, v in d.items() if k != 'offer')) for d in list_of_dicts)
    
    print dictionary
    >>> 
    {1: {'completed__sum': 1, 'offer__count': 2}, 2: {'completed__sum': 0, 'offer__count': 1}}
    

    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.

    dictionary = dict((d.pop('offer'), d) for d in list_of_dicts)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, I want to get a list of User s. My current queryset
Usually in a django view you would do something like queryset = MyModel.objects.something() How
I have a list of objects from a django queryset, e.g. my_list = MyObject.objects.filter(variable=something)
Basically I want to do something similar to annotating a queryset but with a
Using a queryset in Django (in a view) I only want to get said
I have a Django QuerySet , and I want to get a Q object
I've a queryset result, say, animals, which has a list of animals. There are
When I do this queryset: Club.objects.values('district').distinct() I expect to get this SQL: SELECT DISTINCT
Let's say I have a queryset of things Bill has worked on like this:
I would like to make a queryset on a generic view in this way:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.