django: get all columns as a list

Modern way to do this in django >1.9:

[f.get_attname() for f in Restaurant._meta.fields]

You can get the name of all fields this way:

model._meta.get_all_field_names()

or

[field.name for field in model._meta.get_fields()]  # for Django >= 1.9

therefore, you can do something like:

FOO.objects.filter(<your filter>).values_list( * FOO._meta.get_all_field_names(), flat=True)

If you don't want to pass the field names then you can do:

FOO.objects.values_list()

You can see in the reference: "If you don’t pass any values to values_list(), it will return all the fields in the model, in the order they were declared"

Edit

The referenced link is no longer available. It may be viewd on docs.

Tags:

Django