查询结果访问列表django(python) [英] access list from query result on django(python)

查看:91
本文介绍了查询结果访问列表django(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DB table
select * from AAA;

id |  name  | grade
--------------------
 1 |  john  | A
 2 |  cavin | B

django

grade_list = AAA.objects.all()
for item in grade_list:
    print item.name

结果 - >

john
cavin

======================== ================

============================================

我要更改此代码(相同功能)

I want to change this code(same function)

grade_list = AAA.objects.all()
print_field = 'name'

for item in grade_list:
    print item.(  print_field...  )

结果 - >

john
cavin

Q)请填写空白。是否可以?

Q) fill in the blank please. Is it possible?

推荐答案

是的,您可以使用 getattr 内置函数。

Yes you can do this with the getattr built-in function.

grade_list = AAA.objects.all()
print_field = 'name'

for item in grade_list:
    print getattr(item, print_field)

这篇关于查询结果访问列表django(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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