Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda