Skip to content

Instantly share code, notes, and snippets.

@zgoda zgoda/hybridprop.py

Created May 31, 2017
Embed
What would you like to do?
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
You can’t perform that action at this time.