On 10/13/06, climbus <[EMAIL PROTECTED]> wrote:
> Shannon -jj Behrens napisal(a):
> > I've added my notes to the wiki after incorporating all of your feedback:
> >
> > Using SQLAlchemy with Pylons:
> > http://pylonshq.com/project/pylonshq/wiki/SqlAlchemyWithPylons
> >
> > If you update it, please send email to this list so that the rest of
> > us can benefit from your improvements.
> >
> > Best Regards,
> > -jj
> >
> > --
> > The one who gets the last laugh isn't the one who did the laughing,
> > but rather the one who did the writing.
>
> I woluld be intereseted how it'll look if model will go to other file,
> not in __init__py?

I figured there are lots of ways to make this work.  Afterall, this
has more to do with Python than SQLAlchemy or Pylons.  However, since
you asked, I'll provide one straight-forward way:

First of all, I'll assume you're okay with imports that look like:

    from myapp.models.messages import Message

* In models/__init__.py, within init_model, after metadata is created
within the if statement, add:

    from myapp.models.messages import init_model as init_messages
    init_messages(metadata)

* Now, create models/messages.py:

=================================================================
from sqlalchemy import *


class Message(object):

    def __str__(self):
        return ("""
"%(subject)s" posted by "%(name)s" <%(email)s> on "%(created)s\"""" %
            self.__dict__)


def init_model(metadata):

    """Setup the model."""

    global messages_table

    messages_table = Table('messages', metadata,
        Column('message_id', Integer, primary_key=True),
        Column('subject', String(255), nullable=False),
        Column('name', String(255), nullable=False),
        Column('email', String(255), nullable=False),
        Column('created', DateTime, default=func.now(), nullable=False),
        Column('parent', Integer, ForeignKey('messages.message_id')),
        Column('body', TEXT, nullable=False),
    )
    mapper(
        Message, messages_table,
        properties={
            'children': relation(Message, cascade="all")
        }
    )
=================================================================

Basically, once you have the metadata object, you can pass it anywhere
you want.  That means you can define things anywhere you want.

Of course, I didn't actually try this code ;)

Happy Hacking!
-jj

-- 
The one who gets the last laugh isn't the one who did the laughing,
but rather the one who did the writing.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss
-~----------~----~----~----~------~----~------~--~---

Reply via email to