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

PythonDjangoDjango ModelsDjango Queryset

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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

Solution 3 - Python

We can select required fields over values.

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

Solution 4 - Python

Oskar Persson's answer is the best way to handle it because makes it easier to pass the data to the context and treat it normally from the template as we get the object instances (easily iterable to get props) instead of a plain value list.

After that you can just easily get the wanted prop:

for employee in employees:
    print(employee.eng_name)

Or in the template:

{% for employee in employees %}

    <p>{{ employee.eng_name }}</p>

{% endfor %}

Solution 5 - Python

Daniel answer is right on the spot. If you want to query more than one field do this:

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

This will return list of tuples. You cannot use named=Ture when querying more than one field.

Moreover if you know that only one field exists with that info and you know the pk id then do this:

Employee.objects.values_list('eng_name','rank').get(pk=1)

Solution 6 - Python

You can use values_list alongside filter like so;

active_emps_first_name = Employees.objects.filter(active=True).values_list('first_name',flat=True)

More details here

Solution 7 - Python

Employees.objects.filter().only('eng_name')

This will give a QuerySet of all rows, but with only the specified fields. It does NOT give a list like the other answers above. It gives your the OBJECT INSTANCES. Watch out; it is objects.filter().only() NOT just objects.only()

It is similar to SQL Query:

SELECT eng_name FROM Employees;

Solution 8 - Python

queryset = ModelName.objects.filter().only('field1', 'field2')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzentenkView Question on Stackoverflow
Solution 1 - PythonDaniel RosemanView Answer on Stackoverflow
Solution 2 - PythonOskar PerssonView Answer on Stackoverflow
Solution 3 - PythonAlok ChoudharyView Answer on Stackoverflow
Solution 4 - PythonPeteMaikelView Answer on Stackoverflow
Solution 5 - PythonSumit VaiseView Answer on Stackoverflow
Solution 6 - Python7guyoView Answer on Stackoverflow
Solution 7 - PythonMedical Doctor - ProgrammerView Answer on Stackoverflow
Solution 8 - PythonMd Fahad HossainView Answer on Stackoverflow