Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

367
Views
SQLAlchemy - using hybrid properties in a query

I am trying to use a SQLAlchemy hybrid property like this

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 - float(self.value))

Now, I use this in my model like this

class MetricModel(BaseModel):
    def get_dominance(self):
        self.query(Metric).filter(Metric.dominance >  0.5).order_by(Metric.dominance.desc()).limit(2).all()

This is a flask app and it's being called like this

model = MetricModel(db)
with db.session():
    print(model.get_dominant_traits())

This gives me an error TypeError: float() argument must be a string or a number, not 'InstrumentedAttribute' From the error it looks like there is no result set, hence the failure. I followed the docs here http://docs.sqlalchemy.org/en/latest/orm/extensions/hybrid.html What should I do differently?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!