【发布时间】:2017-02-21 05:10:50
【问题描述】:
我是 Flask 的新手,我正在尝试根据我的 Flask 视图中的不同用户事件更新模型。我可以在 shell 中访问它,并且可以提取一些存储在其中的数据的字段。但是我需要根据某些用户事件从视图中更新模型。以下是我的模型文件:
class Package(db.Model):
__tablename__ = 'package'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
student_id = db.Column(db.Integer, ForeignKey('student_profile.id'))
stripe_id = db.Column(db.String(45))
student_email = db.Column(db.String(20))
subscription_date = db.Column(db.DateTime, default=today)
expiry_date = db.Column(db.DateTime, default=deadline)
is_active = db.Column(db.Boolean, default=True)
planname = relationship('Plan', backref=backref('package'))
package_price = db.Column(db.Integer)
coupon = db.Column(db.String(12))
def __init__(self, id,
stripe_id,
student_email,
subscription_date,
expiry_date,
is_active,
planname,
package_price,
coupon):
self.id = id
self.student_id = student_id
self.student_email = student_email
self.subscription_date = subscription_date
self.expiry_date = expiry_date
self.is_active = is_active
self.planname = planname
self.package_price = package_price
self.coupon = coupon
如何在以下视图中更新这些:
from models import Package
@app.route('/somelink', methods=['POST'])
def somefunc():
if studentdidsomething:
planname = 'somename'
db.session.commit(planname)
return render_template('sometemplate.html')
我需要直接使用数据库表,这就是为什么感到困惑,因为烧瓶有多种形式。 Wtf形式,Alchemyforms和just形式。请指教。
【问题讨论】:
标签: python flask flask-sqlalchemy