Skip to main content
 首页 » 编程设计

python之来自 dict 的 sqlalchemy AND 运算符

2025年05月04日9jyk

我遇到了这种奇怪的行为

test_dict = {'productDue' : ['foo'],'releaseDue' : ['bar']} 
for attr, value in test_dict.items() : 
    print attr 
    and_args = [(and_(getattr(my_table,attr).in_(value)))] 

这给了我:

>>> print and_(*and_args) 
"my_table"."releaseDue" IN (:releaseDue_1) 

然后当我切换顺序时:

 test_dict = {'releaseDue' : ['bar'],'productDue' : ['foo']} 
for attr, value in test_dict.items() : 
    print attr 
    and_args = [(and_(getattr(my_table,attr).in_(value)))] 

我得到:

>>> print and_(*and_args) 
"TDC"."releaseDue" IN (:releaseDue_1) 

我不明白,我想要 "TDC"."releaseDue"IN (:releaseDue_1) AND "TDC"."productDue"IN (:productDue_1)

请帮忙

请您参考如下方法:

谢谢,

我已经成功做到了:

and_args = [ (and_(getattr(my_table,attr).in_(value))) for attr, value in test_dict.items() ]