简体   繁体   中英

sqlalchemy AND operator from dict

I am getting this weird behavior

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

This gives me :

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

Then when I switch the order :

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

I get :

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

I don't get it, I want to have "TDC"."releaseDue" IN (:releaseDue_1) AND "TDC"."productDue" IN (:productDue_1)

Help please

Thank you,

I've managed to do it with this :

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

I'm no sqlalchemy expert, but is this hapenning because you are overwriting and_args with each pass of the loop?

Does something like

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)

do the trick?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM