我正在尝试将 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))
这是 Controller :
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'
有人知道我做错了什么吗?
最佳答案
答案如下:
entities.py 底部缺少以下两行:
setup_all()
create_all()
关于python - 从模型对象查询时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4915644/