The paste got a little mangled unfortunately, here's a pastebin of it:

http://pylonshq.com/pasties/1046

On Jan 10, 1:24 pm, Andrey Petrov <sha...@gmail.com> wrote:
> Hi everyone,
>
> For my last few projects, I've been using slightly modified setup in
> my project/model/{__init__,meta}.py and I would like for you to
> consider it for the default package template.
>
> I pasted the diff at the end of the message, but here is a rough
> summary:
>
> Instead of instantiating Session to None in meta.py, I instantiate it
> as a scoped_session object without a bound engine. Then, in
> __init__py, the same init method will bind the Session object to an
> engine.
>
> This has a couple of benefits in that it lets you import the Session
> object prematurely (ie. from +project+.model.meta import Session) and
> it will still work (assuming that the init_model method is called
> before actually using the model). Unlike the current Pylons default
> template where importing too early will cause you to have Session ==
> None.
>
> This is particularly convenient when writing unit tests. It lets you
> do the import normally at the top of the unit test file as you
> normally would, instead of having to do bizarre wrapping in the setUp
> methods.
>
> I have not been able to find any downsides to doing this. No unit
> tests are broken because of this as far as I can tell (although 8 unit
> tests fail from the hg tip for me to begin with).
>
> The diff below contains these changes, it also contains a note about
> SQLAlchemy 0.5+ which deprecates the (autoflush=True,
> transactional=True) parameters in the sessionmaker constructor.
>
> I hope this is helpful!
>
> Thanks for all your great work,
>
> Andrey
>
> Here's pylons-better-sqlalchemy-model-template.diff against hg tip:
> =========== start ===========
> diff -r b98fbafd85c5 pylons/templates/default_project/+package+/model/
> __init__.py_tmpl
> --- a/pylons/templates/default_project/+package+/model/
> __init__.py_tmpl Wed Jan 07 20:56:58 2009 -0800
> +++ b/pylons/templates/default_project/+package+/model/
> __init__.py_tmpl Sat Jan 10 13:09:25 2009 -0500
> @@ -13,10 +13,8
> @@
>      #
> autoload_with=engine)
>      #orm.mapper(Reflected,
> reflected_table)
>
> #
> -    sm = orm.sessionmaker(autoflush=True, transactional=True,
> bind=engine)
> -
>      meta.engine =
> engine
> -    meta.Session = orm.scoped_session
> (sm)
> +    meta.Session.configure
> (bind=engine)
>
>  ## Non-reflected tables may be defined and mapped at module level
> diff -r b98fbafd85c5 pylons/templates/default_project/+package+/model/
> meta.py_tmpl
> --- a/pylons/templates/default_project/+package+/model/
> meta.py_tmpl     Wed Jan 07 20:56:58 2009 -0800
> +++ b/pylons/templates/default_project/+package+/model/
> meta.py_tmpl     Sat Jan 10 13:09:25 2009 -0500
> @@ -1,6 +1,7 @@
>  {{if sqlalchemy}}
>  """SQLAlchemy Metadata and Session object"""
>  from sqlalchemy import MetaData
> +from sqlalchemy.orm import scoped_session, sessionmaker
>
>  __all__ = ['Session', 'metadata']
>
> @@ -8,7 +9,8 @@
>  engine = None
>
>  # SQLAlchemy session manager.  Updated by model.init_model()
> -Session = None
> +# For SQLAlchemy 0.5+ replace the sessionmaker parameters with
> autocommit=True
> +Session = scoped_session(sessionmaker(autoflush=True,
> transactional=True))
>
>  # Global metadata. If you have multiple databases with overlapping
> table
>  # names, you'll need a metadata for each database
> =========== end ===========
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-devel" group.
To post to this group, send email to pylons-devel@googlegroups.com
To unsubscribe from this group, send email to 
pylons-devel+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/pylons-devel?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to