我有模态定义并且能够通过选择查询获得结果:
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)
有可能吗?
答案 0 :(得分:1)
尝试attrgetter:
from operator import attrgetter
field = 'country'
country = 'usa'
User.select(User.email, User.username).where(attrgetter(field)(User)==country)