Goddit !! Thank you so much!!
On 16 June 2010 14:33, Michael Bayer <[email protected]> wrote: > > On Jun 16, 2010, at 7:58 AM, Robert Sudwarts wrote: > > Hi, > > I'm having difficulty understanding how to call/create a session to access > a "class method" of a declaratively defined table/object: essentially my > understanding of how session works in this context is at fault... Clearly, > the @classmethod requires a **session** (and I realise that: "declarative > has no integration built in with sessions, and is only intended as an > optional syntax for the regular usage of mappers and Table objects") but I'm > unsure of how to 'poke' this in. And I'm not sure if "model.py" require the > definition of its own 'local' session, or if/whether this would conflict > with the session instantiated in "controller.py" > > I'd be grateful for any assistance! > > > What I'm trying to achieve is as follows: > > *Module #1 -- model.py* > > from sqlalchemy.ext.declarative import declarative_base > > Base = declarative_base() > > class SomeClass(Base): > __tablename__ = 'some_table' > id = Column(Integer, primary_key=True) > name = Column(String(50)) > > @classmethod > def by_name(cls, name): > return **Session**.query(cls).filter(cls.name==name).first() > > def build_db(): > '''call this to create the database''' > engine = create_engine('sqlite:/// > Base.metadata.create_all(engine=engine) > > *Module #2 - controller.py* > > from model.py import SomeClass > from sqlalchemy import create_engine > from sqlalchemy.orm import sessionmaker, scoped_session > > engine = create_engine('sqlite:///) > Session = scoped_session(sessionmaker(bind=engine, autocommit=False, > autoflush=False)) > > def getUser(username): > return SomeClass.by_name(username) > > > > > your scoped_session would be defined someplace neutral, for example Pylons > puts it in a file called model/meta.py, and your classmethod would access it > directly as a global variable in meta. > > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<sqlalchemy%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/sqlalchemy?hl=en. > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" 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/sqlalchemy?hl=en.
