How to retrieve model column value dynamically in python

后端 未结 2 1592
清酒与你
清酒与你 2020-12-22 05:53

Suppose I have a model object.

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

# this will output column 1 value
print (table.col1)
相关标签:
2条回答
  • 2020-12-22 06:19

    To get attribute value by name use getattr

    getattr(table, 'col1')
    
    0 讨论(0)
  • 2020-12-22 06:25

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

    col = 'col1'
    getattr(table, col)
    
    0 讨论(0)
提交回复
热议问题