Pylons 1.0 AttributeError:'module'对象没有属性'metadata'

时间:2010-06-10 01:40:57

标签: python sqlalchemy metadata pylons attributeerror

Python noob试图学习Pylons。我正在使用1.0文档中的QuickWiki教程(http://pylonshq.com/docs/en/1.0/tutorials/quickwiki_tutorial/),但这个所谓的“1.0”文档似乎只是“0.9.7”;我怀疑这与我得到的错误有关。

当我执行“paster setup-app development.ini”时,我明白了:

(mydevenv)lucid@lucid-laptop:~/QuickWiki$ paster setup-app development.ini
Traceback (most recent call last):
... edited for brevity...
File "/home/lucid/mydevenv/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg/pkg_resources.py", line 1954, in load
File "/home/lucid/QuickWiki/quickwiki/config/middleware.py", line 11, in <module>
from quickwiki.config.environment import load_environment
File "/home/lucid/QuickWiki/quickwiki/config/environment.py", line 12, in <module>
from quickwiki.model import init_model
File "/home/lucid/QuickWiki/quickwiki/model/__init__.py", line 27, in <module>
pages_table = sa.Table('pages', meta.metadata,
AttributeError: 'module' object has no attribute 'metadata'
(mydevenv)lucid@lucid-laptop:~/QuickWiki$

3 个答案:

答案 0 :(得分:4)

这是文档http://pylonshq.com/docs/en/1.0/tutorials/quickwiki_tutorial/

中的错误

像这样声明pages_table

from quickwiki.model.meta import Base
pages_table = sa.Table('pages', Base.metadata,
                sa.Column('title', sa.types.Unicode(40), primary_key=True),
                sa.Column('content', sa.types.UnicodeText(), default='')
                )

没有loger meta.metadata,使用meta.Base.metadata并使用SqlAlchemy声明性基本扩展http://www.sqlalchemy.org/docs/05/ormtutorial.html#creating-table-class-and-mapper-all-at-once-declaratively

定义模型

答案 1 :(得分:2)

您对estin答案的评论询问这是否在SqlAlchemy 0.5和0.6之间发生变化。

没有。一样的。它是现在有不同默认值的Pylons。 正如estin所说,Pylons默认创建一个declarative_base(),以便您可以声明性地使用SqlAlchemy。

class MyRecord(Base):
     __tablename__ = "myrecord"

     id = Column(Integer, primary_key=True)
     data = Column(Unicode, nullable=False)

这不是首先使用Table()结构指定表,然后创建类,然后使用mapper()将它们映射到一起。

SqlAlchemy Declarative会自动执行此操作。 Quickwiki告诉您使用SqlAlchemy的显式非声明性版本,这是没有理由的(声明性更简洁)。 Pylons用于将默认元数据公开为model.meta.metadata,但现在它由declarative_base()创建,并在model.meta.Base.metadata中公开。

答案 2 :(得分:2)

万一有人遇到同样的问题,我包括我的模型。 init 和websetup:

"""=========================__init__.py========================="""
    """The application's model objects"""
from quickwiki.model.meta import Session, Base


def init_model(engine):
    """Call me before using any of the tables or classes in the model"""
    Session.configure(bind=engine)

import logging
import re
import sets
from docutils.core import publish_parts

from pylons import url
from quickwiki.lib.helpers import link_to

log = logging.getLogger(__name__)

# disable docutils security hazards:
# http://docutils.sourceforge.net/docs/howto/security.html
SAFE_DOCUTILS = dict(file_insertion_enabled=False, raw_enabled=False)
wikiwords = re.compile(r"\b([A-Z]\w+[A-Z]+\w+)", re.UNICODE)

from sqlalchemy import orm
import sqlalchemy as sa

pages_table = sa.Table('pages', Base.metadata,
                sa.Column('title', sa.types.Unicode(40), primary_key=True),
                sa.Column('content', sa.types.UnicodeText(), default='')
                )

class Page(object):

    def __init__(self, title, content=None):
        self.title = title
        self.content = content

    def get_wiki_content(self):
        """Convert reStructuredText content to HTML for display, and
        create links for WikiWords
        """
        content = publish_parts(self.content, writer_name='html',
                                settings_overrides=SAFE_DOCUTILS)['html_body']
        titles = sets.Set(wikiwords.findall(content))
        for title in titles:
            title_url = url(controller='pages', action='show', title=title)
            content = content.replace(title, link_to(title, title_url))
        return content

    def __unicode__(self):
        return self.title

    __str__ = __unicode__

orm.mapper(Page, pages_table)

"""=========================websetup.py========================="""

"""Setup the QuickWiki application"""
import logging

import pylons.test

from quickwiki.config.environment import load_environment
from quickwiki.model.meta import Session, Base
from quickwiki import model

log = logging.getLogger(__name__)

def setup_app(command, conf, vars):
    """Place any commands to setup quickwiki here"""
    load_environment(conf.global_conf, conf.local_conf)

    # Create the tables if they don't already exist
    log.info("Creating tables...")
    Base.metadata.create_all(bind=Session.bind)
    log.info("Successfully set up.")

    log.info("Adding front page data...")
    page = model.Page(title=u'FrontPage',
                      content=u'**Welcome** to the QuickWiki front page!')

    Session.add(page)
    Session.commit()
    log.info("Successfully set up.")