Skip to content

Instantly share code, notes, and snippets.

@zgoda
Created May 31, 2017 09:44
Show Gist options
  • Save zgoda/b8043985d1294e51e19af4966a78e06c to your computer and use it in GitHub Desktop.
Save zgoda/b8043985d1294e51e19af4966a78e06c to your computer and use it in GitHub Desktop.
Hybrid property
class Platform(db.Model, ModelMixin):
__tablename__ = 'platform'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(200), nullable=False, index=True)
homepage = db.Column(db.String(200))
description = db.Column(db.Text)
@property
def versions_ordered(self):
return self.versions.order_by(db.desc(PlatformVersion.release_date))
@property
def version_count(self):
return self.versions.count()
@property
def latest_version(self):
return self.versions.order_by(db.desc(PlatformVersion.version_str)).first()
@hybrid_property
def report_count(self):
return sum([x.reports.count() for x in self.versions])
@report_count.expression
def report_count(cls):
return db.select([db.func.count(Report.id)]).\
where(db.and_(Report.platformversion_id==PlatformVersion.id, PlatformVersion.platform_id==cls.id)).\
label('report_count')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment