install sqlalchemy
pip install -U Flask-SQLAlchemy
sql alchemy or
from sqlalchemy import or_
filter(or_(User.name == 'ed', User.name == 'wendy'))
sqlalchemy _in array sqlite
session.query(MyUserClass).filter(MyUserClass.id.in_((123,456))).all()
python sqlalchemy db.session use table name as string
# I needed a way to query by referring to the model's tablename. This is what I came up with:
class Model1(db.Model):
__tablename__ = 'table1' # or whatever, doesn't matter
class Model2(db.Model):
__tablename__ = 'table2'
def table_object(table_name):
tables_dict = {table.__tablename__: table for table in db.Model.__subclasses__()}
return tables_dict.get(table_name)
# Then, use it like this:
model_list = ['table1', 'table2']
for model in model_list:
some_var = db.session.query(table_object(table_name=model)).filter_by(name='something').all()
# The important bit is db.Model.__subclasses__() - gives a list of the model classes (objects?).
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us