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.