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 ?
CodePudding user response:
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.