Pylons 1.0 属性错误:“模块”对象没有属性“元数据”;

发布于 2024-09-05 05:17:48 字数 1109 浏览 3 评论 0原文

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

当我执行“paster setup-appdevelopment.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$

Python noob trying to learn Pylons. I'm using the QuickWiki tutorial (http://pylonshq.com/docs/en/1.0/tutorials/quickwiki_tutorial/) from the 1.0 documentation, but this alleged "1.0" doc seems to just be "0.9.7"; I suspect that this has something to do with the error I'm getting.

When I execute "paster setup-app development.ini", I get this:

(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$

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

微凉 2024-09-12 05:17:48

这是文档中的错误 http://pylonshq.com/docs/en/1.0/教程/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='')
                )

没有记录器meta.metadata,使用meta.Base.metadata并使用SqlAlchemy声明性基本扩展定义模型http://www.sqlalchemy.org/docs/05/ormtutorial.html#creating -表类和映射器一次全部声明式

This is mistake in documentation http://pylonshq.com/docs/en/1.0/tutorials/quickwiki_tutorial/

Declare pages_table like this

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='')
                )

No loger meta.metadata, use meta.Base.metadata and define you models using SqlAlchemy declarative base extension http://www.sqlalchemy.org/docs/05/ormtutorial.html#creating-table-class-and-mapper-all-at-once-declaratively

聊慰 2024-09-12 05:17:48

您对 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 的显式非声明性版本,这是没有理由的(声明性更简洁)。 Pylon 过去将默认元数据公开为 model.meta.metadata,但现在它是由 declarative_base() 创建并在 model.meta.Base.metadata 中公开。

Your comment on estin answer asks whether this changes between SqlAlchemy 0.5 and 0.6.

It does not. It's the same. It is Pylons which have different defaults now.
As estin says Pylons defaults to creating a declarative_base() so that you can use SqlAlchemy declaratively.

class MyRecord(Base):
     __tablename__ = "myrecord"

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

This is instead of specifying first the tables using Table() constructs, then creating your classes and then using mapper() to map them together.

SqlAlchemy Declarative does this automatically. Quickwiki tells you to use the explicit non-declarative version of SqlAlchemy which is there are no reason for (declarative is more concise). Pylons used to expose the default metadata as model.meta.metadata but now it is created by declarative_base() and exposed in model.meta.Base.metadata.

⊕婉儿 2024-09-12 05:17:48

以防万一有人遇到同样的问题,我包括我的 model.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.")

Just in case anyone runs into the same issue, I'm including my model.init and 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.")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文