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

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

    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)
    

提交回复
热议问题