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
Post a Comment