0

我正在尝试将 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'

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

4

2 回答 2

1

答案如下:

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

setup_all()
create_all()
于 2011-02-06T23:46:29.093 回答
0

我不太了解 Elixir,但是将以下内容放入 .ini 中还不够吗?

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

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

于 2011-02-06T21:20:49.573 回答