views:

52

answers:

1

I've been reading the Pylons Book and, having got to the part about Models, realise it's out of date. So I then switched over to the official Pylons documentation for creating Models in Pylons 1.0 - http://pylonshq.com/docs/en/1.0/tutorials/quickwiki_tutorial/

I've followed what they've got and it's still failing.

./blog/model/init.py

"""The application's model objects"""
from sqlalchemy import orm, Column, Unicode, UnicodeText
from blog.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)

class Page(Base):
   __tablename__ = 'pages'

   title   = Column(Unicode(40), primary_key=True)
   content = Column(UnicodeText(), default=u'')


class Page(object):

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

   def __unicode__(self):
      return self.title

   __str__ = __unicode__

orm.mapper(Page, pages_table)

Having two classes with the same name kind of blows my mind... But nevertheless, it's what the tutorial says to do.

When I try to run my code, however, I get:

 28, in <module>
    orm.mapper(Page, pages_table)
NameError: name 'pages_table' is not defined

Sup with this? How can I get this to not fail? :/

+4  A: 

First, you should not declare two classes with same name. How is that supposed to work at all?

Second, you probably would want to read official SQLA docs, not Pylons. Pylons docs are a bit messy after upgrade, and still have a lot of 0.9.7 references. Declarative extension is described here: http://www.sqlalchemy.org/docs/reference/ext/declarative.html

Third, declarative means you do not need to bind class to table, it is done in the class definition.

This is sufficient declaration of the mapping, you can proceed to using it:

class Page(Base):
   __tablename__ = 'pages'

   title   = Column(Unicode(40), primary_key=True)
   content = Column(UnicodeText(), default=u'')

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

   def __unicode__(self):
      return self.title

   __str__ = __unicode__
Daniel Kluev
Agreed: you were rightly suspicious about the advice to create classes with the same name.
Jesse Dhillon
Not long after posting my question, my Google searches turned up http://bitbucket.org/bbangert/quickwiki/src which is the tutorial's code written for Pylons 1.0. Got my code working now.
Steve