简体   繁体   中英

DJANGO 3.0: List out of QuerySet (Basic-Level question)

It is very beginner level question, I know. But can anyone just tell me how to make List out of QuerySet.

I am getting queryset with following query:

sports = Category.objects.all()
print(sports)

Output:

<QuerySet [<Category: Golf>, <Category: Cricket>, <Category: Football>, <Category: Golf>, <Category: Hockey>]>

But what I require , is:

print(sports)

Desired Output:

['Baseball', 'Cricket', 'Football', 'Golf', 'Hockey']

You can do this with list comprehension

[category for category in Category.objects.all() ]

It brought my answer:

[category.type for category in Category.objects.all()]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM