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

后端 未结 6 1102
情话喂你
情话喂你 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:37

    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
    

提交回复
热议问题