Django Query distinct值有效,但我无法使用查询结果 [英] Django Query distinct values works but i cant use the query result
本文介绍了Django Query distinct值有效,但我无法使用查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试过
= Staff.objects.all()。values('person')。distinct()
for k in staff:
j = k.person
print j,k
我得到dict对象没有属性为人为k.person
{'person':778L}
/ pre>
{'person ':779L}
{'person':780L}
{'person':781L}
{'person':782L}`
你知道如何获得人的价值?
解决方案它不是
distinct
而是values()
。values()
给你你所请求的值。要从dict获取属性,您可以使用dict ['attr_name']
。
所以你可以试试这个:
staff = Staff.objects.all()。values('person')。distinct()
for k在工作人员中:
j = k ['person']
print j,k
I have a table column which some values are double or triple written. I want to have a distinct query.
I tried
staff = Staff.objects.all().values('person').distinct() for k in staff: j = k.person print j,k
I get "dict object has not attribute as person" for k.person
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?
解决方案Its not problem with
distinct
but withvalues()
.values()
gives you dict of values you requested. And to get attribute from dict you can usedict['attr_name']
.So you can try this:
staff = Staff.objects.all().values('person').distinct() for k in staff: j = k['person'] print j,k
这篇关于Django Query distinct值有效,但我无法使用查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文