来自dict的sqlalchemy AND运算符

时间:2013-07-16 14:02:45

标签: python sqlalchemy

我得到了这种奇怪的行为

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)

请帮助

2 个答案:

答案 0 :(得分:1)

谢谢,

我设法用这个做到了:

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

答案 1 :(得分:0)

我不是sqlalchemy专家,但这是因为你在循环的每次传递中覆盖and_args这是hapenning吗?

喜欢

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

诀窍?

相关问题