2

我有这些课。

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?

4

1 回答 1

3

buoy.author_id将在buoy实例添加到会话并被session.flush()调用后设置。

于 2013-03-21T12:00:38.460 回答