如何在ponyorm select中遍历getattr?

时间:2019-06-22 15:54:46

标签: python ponyorm

我看到以下可能:

attr_name = 'name'
param_value = 'John'
select(getattr(c, attr_name) for c in Customer)

但是,我得到一个错误:

attrs = ['id', 'name']
select((getattr(c, attr) if for attr in attr_name) for c in Customer)

是否有可能在select语句中执行此操作? (即不拉所有东西,然后只拉我关心的列)

这几乎与此处引用的用例相同:

https://docs.ponyorm.org/api_reference.html#getattr

1 个答案:

答案 0 :(得分:0)

这可以解决问题,但不在select语句之内:

attrs = ['id', 'name']
for attr in attrs:
    select(getattr(c, attr) for c in Customer)