0

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?

1 Answer 1

1

You can use values_list and convert the resulting queryset to list. In case car, spare parts, truck, motor cycle are the name field of your model do it like this...

my_list = list(Post_Sub_Category.objects.values_list('name'), flat=True)

I believe my answer should also work for your edited question.

if 'car' in list(ddd):
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.