Search code examples
pythondjangodjango-modelsdjango-queryset

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


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?


Solution

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