I have the following models:
class User(db.Model):
...
#One user can have many roles, many roles can be on many users
roles = db.relationship('Role', secondary='users_roles', backref=db.backref('users', lazy='dynamic')
class Role(db.Model):
id = ..
name = ..
class Notification(db.Model):
...
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
#a notification can be shown for several roles
shows_for = db.relationship('Role', secondary='notifications_roles', backref=db.backref('user_notifications', lazy='dynamic'))
And I'm trying to create a query to get all the notifications for a user, given his/her roles:
def get_notifications():
#Get the current user's roles:
usr_roles = db.session.query(User).filter_by(id=current_user.id).first().roles
#Get notifications to show for that user
relevant_notifications = (Notification.query.filter(
Notification.shows_for_roles.in_(usr_roles)).all()
Now, this doesnt work (NotImplementedError: in_() not yet supported for relationships. For a simple many-to-one, use in_() against the set of foreign key values.)
How would I go about getting the objects that have one or more roles tied to them that also are in my User object?