【问题标题】:Problem querying from a model object从模型对象查询问题
【发布时间】:2011-06-22 09:16:32
【问题描述】:

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

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

【问题讨论】:

    标签: python mysql sqlalchemy pylons python-elixir


    【解决方案1】:

    答案如下:

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

    setup_all()
    create_all()
    

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2011-05-03
        • 1970-01-01
        • 1970-01-01
        • 2020-01-07
        • 1970-01-01
        • 2020-11-16
        • 1970-01-01
        • 1970-01-01
        • 2018-02-20
        相关资源
        最近更新 更多