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

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


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')


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


We can select required fields over values.

Employee.objects.all().values('eng_name','rank')