Django 查询不同的值有效,但我不能使用查询结果 [英] Django Query distinct values works but i cant use the query result
本文介绍了Django 查询不同的值有效,但我不能使用查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表格列,其中一些值是双倍或三倍写的.我想要一个独特的查询.
I have a table column which some values are double or triple written. I want to have a distinct query.
我试过了
staff = Staff.objects.all().values('person').distinct()
for k in staff:
j = k.person
print j,k
我得到 k.person 的dict 对象没有作为人的属性"
I get "dict object has not attribute as person" for k.person
我得到 k 给了我一个类似
And I get k gives me a result like
{'person': 778L}
{'person': 779L}
{'person': 780L}
{'person': 781L}
{'person': 782L}`
你知道我怎样才能得到人的价值吗?
Do you know how can I get the person value?
推荐答案
distinct
不是问题,而是 values()
的问题.values()
给你你请求的值的字典.要从 dict 获取属性,您可以使用 dict['attr_name']
.
Its not problem with distinct
but with values()
. values()
gives you dict of values you requested. And to get attribute from dict you can use dict['attr_name']
.
所以你可以试试这个:
staff = Staff.objects.all().values('person').distinct()
for k in staff:
j = k['person']
print j,k
这篇关于Django 查询不同的值有效,但我不能使用查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文