我有这些课程。
class User(object):
query = db_session.query_property()
def __init__(self, username, passhash, salt):
self.username = username
self.passhash = passhash
self.salt = salt
users = Table('users', metadata,
Column('id', Integer, primary_key=True),
Column('username', String(60)),
Column('passhash', String),
Column('salt', String)
)
mapper(User, users)
class Buoy(object):
query = db_session.query_property()
author = relationship("User", backref=backref("buoys", order_by=id))
def __init__(self, label, lead, body, author):
self.label = label
self.lead = lead
self.body = body
self.author = author
buoys = Table('buoys', metadata,
Column('id', Integer, primary_key=True),
Column('label', String),
Column('lead', String),
Column('body', String),
Column('author_id', Integer, ForeignKey('users.id'))
)
mapper(Buoy, buoys)
在我的Python Flask代码中,我想创建并保存一个新的浮标:
buoy = Buoy(label, lead, body, current_user)
当我这样做时,buoy.author是正确的但是buoy.author_id是None。请问,当buoy.author已知时,如何自动将current_user.id分配给buoy.author_id?
答案 0 :(得分:2)
buoy.author_id
实例添加到会话并调用buoy
后,将设置 session.flush()
。