2011-02-06 40 views
0

我在用SqlAlchemy(通過Elixir)試圖使用Pylons。從模型對象查詢時出現問題

這裏是我的TESTDB /模型/ entities.py:

from elixir import * 

metadata.bind = "mysql://testdb:[email protected]/testdb" 
metadata.bind.echo = True 

class Post(Entity): 
    text = Field(Unicode(128)) 

這裏是控制器:

import logging 

from pylons import request, response, session, tmpl_context as c, url 
from pylons.controllers.util import abort, redirect 

from testdb.lib.base import BaseController, render 

log = logging.getLogger(__name__) 

from testdb.model.entities import * 

class MainController(BaseController): 

    def index(self): 
     c.posts = Post.query.all() 
     print "Test" 
     return render('/index.mako') 

    def submit(self): 
     post = Post() 
     post.text = request.POST.get['text'] 
     session.commit() 

當我運行應用程序,我得到一個錯誤說:

AttributeError: type object 'Post' has no attribute 'query' 

有人知道我在做什麼錯嗎?

回答

1

的答案如下:

entities.py在底部以下兩行丟失:

setup_all() 
create_all() 
0

我不知道藥劑很好,但是不在.ini中放入以下內容嗎?

sqlalchemy.url = mysql://user:[email protected]/dbname?charset=utf8&use_unicode=0 
sqlalchemy.pool_recycle = 3600 

?之後的所有內容都是爲了避免編碼問題。第二行禁止MySQL關閉非活動連接(實際上它每小時重新連接一次)。

+0

那麼,你完全改變了這個問題後,我的答案看起來有點愚蠢... – 2011-02-07 16:55:23