需要关于关系和过滤的一些帮助。鉴于以下模型,我如何通过Milestone.active_date对项目的里程碑进行排序?
尝试这个之后,我知道我不能用财产来做这件事。我已经尝试了,我得到的错误是它不是一个可映射的表是有道理的。那么,执行此操作的最佳/正确方法是什么?
我已经调查了hybrid_property并加入了但我似乎无法实现这一目标。我在数据库方面相当生疏,并且在使用ORM之前做过很多事情,所以非常感谢。
谢谢!
BW
class MilestoneType(db.Model):
__tablename__ = 'MilestoneType'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(25), nullable=False)
icon_name = db.Column(db.String(255))
def __repr__(self):
return self.name
class Milestone(db.Model):
__tablename__ = 'Milestone'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
milestone_type_id = db.Column(db.ForeignKey(MilestoneType.id))
milestone_type = db.relationship("MilestoneType")
milestone_dates = db.relationship("MilestoneDate", order_by="MilestoneDate.datestamp", lazy="dynamic")
notes = db.Column(db.String(255), nullable=False)
project_id = db.Column(db.Integer, db.ForeignKey('Project.id'), nullable=False)
project = db.relationship("Project")
@property
def active_date(self):
return self.milestone_dates.first()
def __repr__(self):
return str(self.name) + ' (Type: ' + str(self.milestone_type) + ')'
class MilestoneDate(db.Model):
__tablename__ = 'MilestoneDate'
id = db.Column(db.Integer, primary_key=True)
milestone_id = db.Column(db.Integer, db.ForeignKey('Milestone.id'), nullable=False)
milestone = db.relationship("Milestone")
datestamp = db.Column(db.DateTime, nullable=False)
notes = db.Column(db.String(255), nullable=False)
def __repr__(self):
return str(self.datestamp)
class Project(db.Model):
__tablename__ = 'Project'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
active = db.Column(db.Boolean, nullable=False, default=True)
name = db.Column(db.String(50), nullable=False)
code = db.Column(db.String(8), nullable=False)
responsible = db.Column(db.String(50), nullable=False)
customer_id = db.Column(db.ForeignKey(Customer.id))
customer = db.relationship("Customer")
project_type_id = db.Column(db.ForeignKey(ProjectType.id))
project_type = db.relationship("ProjectType")
product_id = db.Column(db.ForeignKey(Product.id))
product = db.relationship("Product")
site_id = db.Column(db.ForeignKey(Site.id))
site = db.relationship("Site")
milestones = db.relationship("Milestone", lazy="dynamic")
def __repr__(self):
return str(self.customer) + " - " + str(self.name) + '(' + str(self.product) + ')'