如何在python中动态检索模型列值 [英] How to retrieve model column value dynamically in python

查看:31
本文介绍了如何在python中动态检索模型列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个模型对象.

Suppose I have a model object.

print (dir(table))
['...', 'col1', 'col2', '...']

# this will output column 1 value
print (table.col1)

我想动态地做,例如:

col = 'col1'
table.col

谢谢

推荐答案

在python中做动态属性检索时想使用getattr:

You want to use getattr when doing dynamic attribute retrieval in python:

col = 'col1'
getattr(table, col)

这篇关于如何在python中动态检索模型列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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