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

后端 未结 6 1161
情话喂你
情话喂你 2021-01-30 12:20

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 quer

6条回答
  •  情话喂你
    2021-01-30 12:42

    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 %}
    
        

    {{ employee.eng_name }}

    {% endfor %}

提交回复
热议问题