Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
Get the intersection of ORM Objects in SQLAlchemy

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?

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda