简体   繁体   中英

How to query sqlalchemy object with “IN” in python?

I have a model like

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(32), unique=True, nullable=False)
    character = db.Column(db.String(10), nullable=False)

I also have a list of characters chars = [char1, char2, ...]

In SQL I can say

select * from User
where character in ('char1', 'char2',...)

How can I write this is python?

Based on similar questions asked here I tried:

foo = User.query.filter_by(User.character.in_(chars)).all()

but this gives me the error filter_by() takes 1 positional argument but 2 were given .

What am I doing wrong?

Something like this maybe:

session.query(User).filter(User. character.in_(chars)).all()

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