添加与pyramid_blogr模型的关系的示例

时间:2015-06-29 22:05:51

标签: python pyramid

http://pyramid-blogr.readthedocs.org/en/latest/处完成教程,并希望在用户和条目模型之间添加关系,然后在添加新帖子时填充该FK字段。

这是我到目前为止所拥有的......

entry.py

...
class Entry(Base):
    __tablename__ = 'entries'
    id = Column(Integer, primary_key=True)
    title = Column(Unicode(255), unique=True, nullable=False)
    body = Column(UnicodeText, default=u'')
    created = Column(DateTime, default=datetime.datetime.utcnow)
    edited = Column(DateTime, default=datetime.datetime.utcnow)
    user_id = Column(Integer, ForeignKey('users.id'))

    user = relationship("User", backref=backref('entries'))
...

user.py

...
class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
    name = Column(Unicode(255), unique=True, nullable=False)
    password = Column(Unicode(255), nullable=False)
    last_logged = Column(DateTime, default=datetime.datetime.utcnow)
    entries = relationship("Entry", order_by="entry.id", backref="user_id")

...

我假设forms.py还需要修改以将user_id作为隐藏字段传递,但我可能没有正确考虑这一点。

0 个答案:

没有答案