Reputation: 294
I have the following object result from query set on a model as follow:
ddd = Post_Sub_Category.objects.filter(
category_name__category_name__iexact=dd
).values_list('sub_category_name', flat=True)
the query set I obtained:
<QuerySet ['car', 'spare parts', 'truck', 'motor cycle']>
then tried:
print(ddd.values('sub_category_name'))
I obtained the following result:
<QuerySet [
<Post_Sub_Category: car>,
<Post_Sub_Category: spare parts>,
<Post_Sub_Category: truck>,
<Post_Sub_Category: motor cycle>
]
How to access the values only and make list of them as string:
['car','spare parts','truck','motor cycle'].
the first query set seems that it gave me what I want. However, When I use following if statement. it does not executed:
if 'car' in ddd:
# do some thing
as you can see car
should be in the list. so, I could not understand why the if statement has not been executed.
any help or suggestion?
Upvotes: 1
Views: 138
Reputation: 3847
I think you might need to index that. The values list actually looks like it returns a list. Can you try:
try:
ddd = ddd[0]
except IndexError:
# Catch index error
pass
if 'car' in ddd:
# do some thing
If that doesn't work, try explicitly converting your QuerySetList to a regular ole list, as in this question, like this:
ddd = list(ddd)
Also, this looks a little strange to me: category_name__category_name__iexact=dd
. Posting your corresponding models would be helpful.
Upvotes: 2