python 3.x - Django creating aliased query outputs -


in question, working query gave output:

[{ 'type': 'o', 'count': 54},{ 'type': 'e', 'count': 125},{ 'type': 'c', 'count': 2}] 

one of respondents asked why didn't shortcut this:

{ 'o': 54, 'e': 125...} 

while original question nicely answered pure genius, thought of persons input still in mind.

how "alias" field in query such as:

models.subscription.objects.values("subscription_type").annotate(c=count("subscription_type")) 

to genius recommending? thanks

you can transform data in python this:

result=[{ 'type': 'o', 'count': 54},{ 'type': 'e', 'count': 125},{ 'type': 'c', 'count': 2}]  result_as_dict=dict([(type_and_count['type'], type_and_count['count']) type_and_count in result])  print(result_as_dict) 

maybe there solution in django orm. don't know it.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -