简体   繁体   English

如何在Django中仅检索一个表字段的值

[英]How to retrieve the values for only one table field in Django

If I have following database fields: id, name, emp_id . 如果我具有以下数据库字段: id, name, emp_id

How do I make a query in Django to get the values of column name only with a where clause. 如何在Django中进行查询以仅通过where子句获取列name的值。

Thanks... 谢谢...

Model.objects.filter(...).values('name')

In addition to the answer provided by Ignacio Vazquez-Abrams, you could also use values_list to get the names in a flat list (instead of a dictionary). 除了Ignacio Vazquez-Abrams提供的答案外,您还可以使用values_list来获取平面列表中的名称(而不是字典)。

Model.objects.filter(...).values_list('name', flat=True)

See the documentation for values_list . 请参阅有关values_list文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM