dev
dev

Reputation: 145

how to extract value in queryset and convert to string

the output of my code is currently

book_name = book.objects.values('book_name').filter(book_id=book_id) book_name =str(book_name[0])

this code should give me 'Chronicles of Narnia '.
but it instead returns {'book_name': 'Chronicles of Narnia '}. how do i extract the value i need.

*note there will only be 1 value in this query every time

Upvotes: 0

Views: 1294

Answers (1)

funnydman
funnydman

Reputation: 11376

You could use values_list, option flat will mean the returned results are single values:

book.objects.values('book_name')
.filter(book_id=book_id).values_list('book_name', flat=True)
# <QuerySet [book_name1, book_name2, book_name3, ...]>

Upvotes: 2

Related Questions