'Django format a request in a list
I need help to format a response of a django request, This is a exemple :
With a model like this:
Book(models):
name = string
author = string
release = date
A request :
Book.objects.filter(author='Hergé').value('name')
I got :
[{'name':'Tintin au Tibet'}, {'name':'Tintin au Congo'}, {'name':'Tintin chez les Picarros'}]
I want this :
['Tintin au Tibet','Tintin au Congo','Tintin chez les Picarros']
My question: How to I get what I want by changing only the request ?
Solution 1:[1]
You can work with .values_list(…) [Django-doc]:
Book.objects.filter(author='Hergé').values_list('name', flat=True)
But usually it is not a good idea to use .values(…) or .values_list(…) to retrieve data, not even to serialize: it erodes the model layer, and thus will prevent fetching related objects, obtaining the display name, etc.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Willem Van Onsem |
