Flask-SQLAlchemy query many-to-many tagging with multiple requred tags Flask-SQLAlchemy query many-to-many tagging with multiple requred tags flask flask

Flask-SQLAlchemy query many-to-many tagging with multiple requred tags


Might not be the most efficient (if you have many tags to search), but very readable way to compose the query:

input_tags = ['selfie', 'summer']q = db.session.query(Photo)for tag in input_tags:    q = q.filter(Photo.tags.any(Tag.name == tag))

For incomplete use startswith(..) instead of ==:

input_tags = ['sum', 'fu']q = db.session.query(Photo)for tag in input_tags:    q = q.filter(Photo.tags.any(Tag.name.startswith(tag)))