peewee中的动态字段查询

时间:2017-12-26 09:56:50

标签: python sql peewee

我有模态定义并且能够通过选择查询获得结果:

country = 'usa'
User.select(User.email, User.username).where(User.country==country)

我想过滤这个动态字段名称,例如:

field = 'country'
country = 'usa'
User.select(User.email, User.username).where(User[field]==country)

有可能吗?

1 个答案:

答案 0 :(得分:1)

尝试attrgetter:

from operator import attrgetter


field = 'country'
country = 'usa'
User.select(User.email, User.username).where(attrgetter(field)(User)==country)