Skip to main content

TurboGears 簡單存取資料庫的方法

· One min read

拿到一個現成的資料庫,最快速的就是直接存取啦.

TurboGears 中除了預先定義 schema 外,也可以直接使用 SQLAlchemy 中的 SqlSoup 模組來直接存取資料庫.

之前我有寫過英文版的,今天 trace 一下 sqlsoup 後發現還有更簡單的方法:直接取用 metadata.

1. 修改 dev.cfg 中的 sqlalchemy.dburi , 指到對應的資料庫

2. 在 your project/model.py 中,寫下

from turbogears.database import metadata from sqlalchemy.ext.sqlsoup import SqlSoup

soup = SqlSoup(metadata)

3. 使用 shell 存取資料

$ tg-admin shell

poll = soup.poll.select() print poll

TurboGears with SqlSoup

· 2 min read

Purpose

Have an exist database and want migrate to TurboGears? Or you've been using TurboGears SQLObject model and want to migrate to SQLAlchemy?

Solution

TurboGears newly support SQLAlchemy 0.2 module(>0.9a6), it provide a slick solution: SqlSoup !

What SqlSoup does?

SqlSoup inspects your database and reflects its contents to class, no model definitions are necessary!

What does that mean?

It means you only need to specify a uri for database, and call SqlSoup to do the rest.

What's the Benifits?

SqlSoup maps database tables to classes automatically. I'm not sure how efficient it is, but at least you can use SqlSoup by following ways:

  • To use existing Database design tools to design my database
  • To use SqlSoup in design phase, and code to SQLAlchemy class when the databse is settled down.
  • To painless migrate SQLObject generated database to SQLAlchemyTest Code

Try the test code, it is thin.

in model.py

1 from sqlalchemy.ext.sqlsoup import SqlSoup

2 uri = "sqlite://%(current_dir_uri)s/devdata.sqlite" 3 soup = SqlSoup(uri)

Explain

line 1 import SqlSoup from sqlalchemy module,

line 2 specify the uri link to the database (I believe it can be improved)

line 3 the real code calling SqlSoup connect to uri

line 4 and line 5 shows I call the table "poll" from database

if you can't execute is , maybe you should install sqlalchemy first

$ easy_install sqlalchemy

or try the TurboGears way:

1 from turbogears import config 2 from sqlalchemy.ext.sqlsoup import SqlSoup

3 uri = config.get('sqlalchemy.dburi') 4 soup = SqlSoup(uri) All you need to do is specify the dburi for sqlsoup.

Let's test what we got

poll = soup.poll.select() print poll

Reference

That's all you need in TurboGears. To get further usage , you can refer to Introducing SqlSoup

ps: Actually TurboGears has done nothing particularly to support SqlSoup, I just use it as other modules.

Python FAQ Zone

· One min read

剛看到一個 Python FAQ 系統. 當然這並不是最好的 FAQ 系統,不過裡面除了一般的分類索引之外,還有個非常有趣的功能是 "Show me a random article! (隨機文章)", 這功能有點像一些 IDE 剛開啟的時候出現隨機的 FAQ 視窗,每次開啟時都有不同的訊息.

TurboEntity 版 SimpleBlog 之二

· 2 min read

TurboEntity 照著 'SimpleBlog Part II' 中的範例改寫後,獲得以下程式碼 (model.py):

>
> from turboentity import *
> from docutils.core import publish_parts
>
> class Post(Entity):
> class turboentity:
> tablename = "posts"
>
> title = Column(Unicode(50))
> content = Column(Unicode)
> post_date = Column(DateTime, default=datetime.now())
> is_published = Column(Boolean, default=False)
> comments = OneToMany('Comment',backref='posts')
>
> @property
> def html_content(self):
> return publish_parts(self.content,writer_name="html")["html_body"]
>
> class Comment(Entity):
> class turboentity:
> tablename = "comments"
>
> author_name = Column(Unicode(255), nullable=False)
> author_email = Column(Unicode(255), nullable=False)
> author_url = Column(String(255))
> comment_date = Column(DateTime, default=datetime.now())
> content = Column(Unicode)
> post = ManyToOne("Post",backref='comments')
>

要表示 post-comments 關係只需分別在兩個類別中宣告 OneToMany - ManyToOne 即可,真是方便呀.

另外當定義好 TurboEntity 類別後,物件自動可以用傳值的方式輸入內容,不需一個個各別指定喔.

例如一般的寫法是這樣子:

p = Post.get (1) # 取得條目 c = Comment () # 新建一條評論 c.post = p # 這條評論屬於 p 條目 c.author_name = "CommentDude1" # 這條目的作者是 c.author_email = "[email protected]" # 這條目作者的郵件信箱是 c.content = "Great post! Keep them coming!" # 這條目的內容 c.flush () 可以直接改成 p = Post.get (1) c = Comment (post=post, author_name="Bob", author_email="[email protected]", content="Bob loves this site.", author_url="http://bob.example.com/") c.flush()

TurboEntity改寫的程式和原本 Tutorial 相比,可以看出它好用的地方.