SQLAlchemy - using hybrid properties in a query

前端 未结 1 2017
一个人的身影
一个人的身影 2021-01-04 03:13

I am trying to use a SQLAlchemy hybrid property like this

class Metric(Base):
    __tablename__ = \'metric\'
    id = Column(Integer, primary_key=True)
    v         


        
相关标签:
1条回答
  • 2021-01-04 04:14

    You need to create expression

    from sqlalchemy import func
    
    class Metric(Base):
        __tablename__ = 'metric'
        id = Column(Integer, primary_key=True)
        value = Column(Float, nullable=False)
    
        @hybrid_property
        def dominance(self):
            return 1 - abs(0.5 - self.value)
    
        @dominance.expression
        def dominance(cls):
            return 1 - func.abs(0.5 - cls.value)
    
    0 讨论(0)
提交回复
热议问题