Tornado Documentation
http://www.v2ex.com/tornado/
Tornado on GitHub
https://github.com/facebook/tornado/
Tornado Gists
http://tornadogists.org/
qloog
V2EX  ›  Tornado

大家在 tornado 中,sqlalchemy 获取新闻及其分类是如何使用的?

  •  
  •   qloog · Jun 24, 2014 · 5450 views
    This topic created in 4403 days ago, the information mentioned may be changed or developed.
    我的表结构大致是这样的:
    表news:
    news_id
    category_d
    title
    content
    create_time
    status

    --------
    表news_category:
    category_id
    category_name

    如果想实现 获取“新闻列表”的时候同时获取到category_name呢? 通过循环category_id获取貌似太傻,好像sqlalchemy 建立model或者query时可以做到。

    但是用哪种方法更适合,或者有其他比较推荐的做法么?

    关于 sqlalchemy 貌似还不太会用~~, 请各位大神指导下吧!
    另外:这种建表方法应该没问题吧(还是说通过单独的关联表更好呢?)
    4 replies    2014-07-25 14:27:02 +08:00
    messense
        1
    messense  
       Jun 24, 2014
    qloog
        2
    qloog  
    OP
       Jun 24, 2014
    @messense 主要是看不懂
    shajiquan
        3
    shajiquan  
       Jul 25, 2014
    class News(Base):
    category_id = Column(Integer, ForeignKey('news_category .category_id'))
    category = relationship("news_category")
    ...


    news = db_session.query(News).filter_by(id=5).first()

    分类名字:
    news.category.category_name
    shajiquan
        4
    shajiquan  
       Jul 25, 2014
    呃,我的缩进呢……
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1099 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:19 · PVG 02:19 · LAX 11:19 · JFK 14:19
    ♥ Do have faith in what you're doing.