On Mar 31, 2008, at 4:49 PM, johnnyice wrote:

>
> What do you recommend instead of scopedsession?

definitely use ScopedSession to manage your Sessions on a thread-local  
basis.    Im talking about a specific "feature" of SS called  
"ScopedSession.mapper", described here:  
http://www.sqlalchemy.org/docs/04/session.html#unitofwork_contextual_associating_autosave
 
  , which "autosaves" all objects created to that session.   Its this  
particular feature I've never liked but we're sort of stuck with it  
for various reasons.

Usually, people use ScopedSession.mapper to get a variable "query" on  
their classes which produces a Query object.   A more explicit way to  
set this up is to say:

MyClass.query = some_scoped_session.query_property()

Usually I advise people to create a base class for their application  
which carries whatever attributes they'd like their application's  
model classes to have, such as this one which sets up "query" as well  
as a default constructor:

Session = scoped_session(sessionmaker(autoflush=True,  
transactional=True))

class Base(object):
        query = Session.query

        def __init__(self, **kwargs):
                for key in kwargs:
                        setattr(self, key, kwargs[key])


then your model classes just look like:

class MyClass(Base):
        # ...

The above approach is particuarly convenient in conjunction with the  
declarative layer too, since in that case you're using a common base  
class anyway.


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to