Fix Python – How to obtain a QuerySet of all rows, with specific fields for each one of them?

Question

Asked By – zentenk

I have a model Employees and I would like to have a QuerySet of all rows, but with some specific fields from each row, and not all fields.

I know how to query all rows from the table/model:

Employees.objects.all()

I would like to know how to select fields for each of the Queryset element. How can I do that?

Now we will see solution for issue: How to obtain a QuerySet of all rows, with specific fields for each one of them?


Answer

Employees.objects.values_list('eng_name', flat=True)

That creates a flat list of all eng_names. If you want more than one field per row, you can’t do a flat list: this will create a list of tuples:

Employees.objects.values_list('eng_name', 'rank')

This question is answered By – Daniel Roseman

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0