来自di的sqlalchemy和操作符

2024-10-01 02:29:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这种奇怪的行为

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)

请帮帮我


Tags: andintestfoovaluemytableargs
2条回答

我不是sqlalchemy专家,但这是不是因为每次循环都要覆盖和参数?你知道吗

是这样的吗

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)

你有本事吗?你知道吗

谢谢你

我设法做到了:

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

相关问题 更多 >