When a query returns a set of records, values_list returns a list of values. A json or a dictionary is more useful when the data needs to be sent as an http json response

Description

When a query returns a set of records, values_list returns a list of values. A json or a dictionary is more useful when the data needs to be sent as an http json response
For eg:
values = list(TestModel.objects.values_list('clustering_key', flat=True))
returns [19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 0L]

values = list(TestModel.objects.values_list('clustering_key','name'))
returns [[19L,abc], [18L,def], [17L,ghi]]

Returning a json is more useful because http calls quite often expect json responses
{
{
'clustering_key':19L,
'name':abc
}
{
'clustering_key':18L,
'name':def
}
}

It becomes hard for the user to always convert a list to a json as there is no in-built method for it.
A method for returning a json or a dict(which can easily be converted to a json) would be very helpful

Environment

None

Pull Requests

None

Activity

Show:
Jim Witschey
May 12, 2018, 7:04 AM

This sounds like a good feature to have, and one that could easily build on the existing values_list method. I think it might be more appropriate to leave values_list as is and provide a new method for it; maybe .values_map. , what do you think? , would that solve your problem?

In case you don't have a converter function in place, a succinct way to deal with this could be something like

(I haven't tested that, but that's the comprehension I reach for)

Alan Boudreault
May 13, 2018, 12:16 AM

I agree that it might be better to add this feature. I think that implementing something similar to Django would be good: .values() returns dictionaries. (and .values_list() is used for list and list of nametuple). +1

Assignee

Unassigned

Reporter

Anusha Mulumoodi

Fix versions

None

Labels

PM Priority

None

External issue ID

None

Doc Impact

None

Reviewer

None

Size

None

Pull Request

None

Sprint

Py P-DEF

Priority

Minor