How to obtain a QuerySet of all rows, with specific fields for each one of them?
We can select required fields over values.
Employee.objects.all().values('eng_name','rank')
In addition to values_list
as Daniel mentions you can also use only
(or defer
for the opposite effect) to get a queryset of objects only having their id and specified fields:
Employees.objects.only('eng_name')
This will run a single query:
SELECT id, eng_name FROM employees
Employees.objects.values_list('eng_name', flat=True)
That creates a flat list of all eng_name
s. 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')