Problem querying from a model object

1.1k Views Asked by At

I'm trying to use Pylons with SqlAlchemy (through Elixir).

Here is my 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))

And here is the 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()

When I run the application, I get an error saying:

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

Does someone know what I'm doing wrong?

2

There are 2 best solutions below

0
On BEST ANSWER

The answer is as follows:

entities.py is missing the following two lines at the bottom:

setup_all()
create_all()
0
On

I don't know Elixir well, but isn't putting the following in the .ini enough?

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

All the stuff after ? is to avoid encoding problems. The second line prevents MySQL from closing inactive connections (in fact it reconnects every hour).