Python Django - load a column from a database into a list

How to load a database column into a list using Django? I have a column "name" of various names in my database, and I want to be able to load all these names (sorted by id) into a list. So that I can iterate over this list and print the names, for example:

for name in name_list: print name 

I searched this quite carefully and I can’t find anything. It should be that simple, it's just the equivalent of SQL "SELECT column FROM table"

 class chat_messages(models.Model): name = models.CharField(max_length=32) 
+7
source share
1 answer

Mark the docs for values_list() . Django does a great job of creating a request specific to your request.

 chat_messages.objects.all().values_list('name') 

Generates a query similar to:

 SELECT `projectname_chat_messages`.`name` FROM `projectname_chat_messages` 
+15
source

All Articles