如何说服 Pygal 从 SQLAlchemy 数据库中的 2 个表中提取数据?
这些是我的模型:
class PlannedPost(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(30), nullable=False, default='planned')
category = db.Column(db.String(30), nullable=False, default=None)
name = db.Column(db.String(30), nullable=True)
planned_amount = db.Column(db.Float, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
date_period = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
comments = db.Column(db.Text, nullable=True)
def __repr__(self):
return f"Post('{self.title}, '{self.category}'\
, '{self.name}', '{self.planned_amount}'\
, '{self.date_period}', '{self.comments}')"
class ActualPost(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(30), nullable=False, default='actual')
category = db.Column(db.String(30), nullable=False, default=None)
actual_amount_name = db.Column(db.String(30), nullable=True)
actual_amount = db.Column(db.Float, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
comments = db.Column(db.Text, nullable=True)
def __repr__(self):
return f"ActualPost('{self.title}, '{self.category}'\
, '{self.actual_amount_name}', '{self.actual_amount}'\
, '{self.date_posted}', '{self.comments}')"
这就是我梦想的路线,我知道这是错误的,因为 Pygal 似乎只能使用列表。
@posts.route("/graphing")
def graphing():
planned = PlannedPost.query
actual = ActualPost.query
graph = pygal.Bar()
graph.title = 'Planned Values vs Actual Values'
graph.x_labels = planned.title
graph.add('Planned', [planned.planned_amount])
graph.add('Actual', [actual.actual_amount])
graph_data = graph.render_data_uri()
return render_template('graphing.html', graph_data=graph_data)