-
Notifications
You must be signed in to change notification settings - Fork 16
Open
Labels
Description
I watched a consumer of an API using this iterate over and over on a list of dicts for 1 field. In the Django ORM, this is where something like foo_queryset.values_list('name', flat=True) would be a no brainer to make it easier to use the results.
Based on the conversation I've already had with @wimglenn this seems to be the blessed approach:
fields_listinstead offieldswill return an array of arrays (list of lists) since this is JSON we're talking aboutfields_listqueries can have an optionalflatquery argument which will flatten the array of arrays into a single array containing the results akin toflat=Truewill when calling a queryset with values_list()- Due to keys being inherently unordered,
fields_list!=foo,bar,bazwill not be supported as there is no way to predetermine the field ordering. - Any request using multiple fields along with
flatshould return a HTTP 400 Bad Request e.g:fields_list=foo,bar&flatis invalid.
So given data from this request:
GET /api/animals/?fields=name,type
{
count: 2,
next: null,
previous: null,
results: [
{
name: "wombat",
type: "marsupial"
},
{
name: "wallaby",
type: "marsupial"
}
]
}A query such as:
GET /api/animals/?fields_list=name,type
Would return data such as:
{
count: 2,
next: null,
previous: null,
results: [
["wombat", "marsupial"],
["wallaby", "marsupial"]
]
}GET /api/animals/?fields_list=name&flat or GET /api/animals/?fields_list=name&flat=true
Would return data such as:
{
count: 2,
next: null,
previous: null,
results: [
[
"wombat",
"wallaby"
],
]
}Reactions are currently unavailable