从模型对象查询的问题

时间:2011-02-06 19:42:36

标签: python mysql sqlalchemy pylons python-elixir

我正在尝试使用Pylons和SqlAlchemy(通过Elixir)。

这是我的testdb / model / entities.py:

from elixir import *

metadata.bind = "mysql://testdb:hundertwasser@localhost/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'

有人知道我做错了什么吗?

2 个答案:

答案 0 :(得分:1)

答案如下:

entities.py在底部缺少以下两行:

setup_all()
create_all()

答案 1 :(得分:0)

我不太了解Elixir,但是没有将以下内容放在.ini中吗?

sqlalchemy.url = mysql://user:pass@localhost/dbname?charset=utf8&use_unicode=0
sqlalchemy.pool_recycle = 3600

?之后的所有内容都是为了避免编码问题。第二行阻止MySQL关闭非活动连接(事实上它每小时重新连接一次)。