I just started experimenting with .4 last night, and I'm really
jazzed. The tutorials have been helpful, but I'm struggling to figure
out how to bridge the gap between the tutorial fragments and a real-
world application.

So far, I've got a little idea working, and I'm hoping to get some
feedback if I'm working in the right direction. I'm trying to
encapsulate my SA logic in its own module and work with these objects
in my programs.

Below is a very simplified example of what I'm trying to do. I could
be handling my sessions, metadata, and tables poorly/inefficiently,
and I'd love some feedback where it could be better.

One glaring problem is the handling of the session information. I
tried to put it into a __get_session() method of ModelHandler, but I
was having trouble getting it working when called from a subclass. (b/
c User has no method __get_session())

It also seems that the four lines under my classes (creating the
engine, metadata, mapping, etc.) could be put into the constructor of
my superclass, but I'm not sure how to refrerence it yet.

Thanks in advance - I'm really looking forward to diving deeper with
SA!

*** Models.py ***

from sqlalchemy import *
from sqlalchemy.orm import mapper
from sqlalchemy.orm import sessionmaker

class ModelHandler(object):
    def save(self):
        # This session stuff should probably be handled by a private
method
        # but I'm having trouble getting it to work when save is
subclassed()
        Session = sessionmaker(bind=db, autoflush=True,
transactional=True)
        session = Session()
        session.save(self)
        session.commit()
        print "Debugging Statement: Saved User"

class User(ModelHandler):
    def __init__(self, name, fullname, password):
        self.name = name
        self.fullname = fullname
        self.password = password

# There should be some tidy place to put these in my objects
db = create_engine('postgres://apache:@localhost:5432/test')
metadata = MetaData(db)
users_table = Table('users', metadata, autoload=True)
mapper(User, users_table)


>>> from models import *
>>> new_user = User('wendy', 'Wendy Williams', 'foobar')
>>> new_user.save()


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to