Created
May 31, 2017 09:44
-
-
Save zgoda/b8043985d1294e51e19af4966a78e06c to your computer and use it in GitHub Desktop.
Hybrid property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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