django模型选择单个字段 [英] django models selecting single field

查看:86
本文介绍了django模型选择单个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Employees 的表/模型,我想将单个字段的所有行作为查询集。

I have a table/models called Employees and I would like to get all rows of a single field as a queryset.

我知道我可以这样做(希望我也这样做):

I know I can do it like this (hope I'm doing this right even):

emp_list = Employees.objects.get(all)
emp_names = emp_list.eng_name

将查询数据库对于所有字段,仅使用一个?有没有更好的方法(更快)?

Would query the database for all fields and using only one? Is there a better (faster) way of doing this?

推荐答案

Employees.objects.values_list('eng_name', flat=True)

这将创建所有<$ c $的固定列表c> eng_name s。如果您希望每行不止一个字段,则不能做一个平面列表:这将创建一个元组列表:

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

这篇关于django模型选择单个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆