Reputation: 485
I wrote this query
p_name = OrderLine.objects.filter(order_id=order).values('product_name')
and it is returning the following results
<QuerySet [{'product_name': 'doc 1 (1-1000)'}]>
I want to use only doc 1 (1-1000)
as a string. Is there a method for this. I read on some website to use .values()
but is returning [{'product_name': 'doc 1 (1-1000)'}]
Upvotes: 0
Views: 47
Reputation: 11
I agree with @Lemayzeur answer. But you can also use the code as you have it, in two ways:
p_name.first() # this will return the dictionary itself
Or you can convert the queryset into a list using
list(p_name)
It's not the recommended way, but it may come handy in other situations.
Cheers!
Upvotes: 0
Reputation: 8525
You could use values_list()
+ flat=True
to generate a list instead.
>>> p_name = OrderLine.objects.filter(order_id=order).values_list('product_name',flat=True)
>>> print(p_name)
>>> <QuerySet ['doc 1 (1-1000)']>
This is meant to generate a list of values from the field passed in value_list()
. In case you are sure that this will have only one value and it's the one wanted, you may index the item in the list:
>>> p_name[0]
>>> 'doc 1 (1-1000)'
Upvotes: 1