I have two tables. Foo and Bar.
class Foo(db.Model):
id = db.Column(db.Integer, primary_key=True)
bars = db.relationship('Bar', backref='foo', lazy='dynamic')
class Bar(db.Model):
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.DateTime, default=datetime.utcnow)
foo_id= db.Column(db.Integer, db.ForeignKey('foo.id'))
How can I have a hybrid property in Foo that can find the average date from all the bars. I have tried the following, which doesn't produce any errors but doesnt give the desired outcome
@hybrid_property
def baz(self):
return db.select([db.func.avg(Bar.date)]) \
.where(Bar.foo_id == self.id) \
.label('baz')
What am I doing wrong?