3

I created the following query:

my_books = Book.objects.values('title') 
print(my_books)

The output is:

<QuerySet [{'title': 'It do go down'}, {'title': 'Second Post'}, {'title': 'Tesla - A New Generation'}]>

Now I want to change the QuerySet to give me back a list which should look like this:

['It do go down', 'Second Post', 'Tesla - A New Generation']

What do I have to do in order to get the result above?

PS: What I tried was an answer to a related question: print(list(my_books)) which showed the following:

[{'title': 'It do go down'}, {'title': 'Second Post'}, {'title': 'Tesla - A New Generation'}]

Furthermore I tried an iteration, which didn't work at all.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
lambrini2002
  • 33
  • 1
  • 5

1 Answers1

10

If you want a list, you should use values_list.

my_books = Book.objects.values_list('title', flat=True) 

The flat=True parameter makes it a flat list, rather than a list of lists.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895