iain duncan wrote:
> On Sat, 2007-15-12 at 19:39 -0500, Mark Ramm wrote:
>>>> Here's what I figure I have to get in there to port my work so far:
>>>>
>>>> - SA 0.4 with assign_mapper equivalent syntax ( Foo.query.get_by() )
>>> You'll need to use the DBSession.mapper (which is a contextual mapper)
>>> for this (just in case you haven't figured that out already)
>> To be a bit more specific, I believe the mapper we use is actually
>> contextual by default, but you should just use DBSession directly, and
>> not instantiate it becaus DBSession acts transparently as a proxy to a
>> thread-local session.
> 
> Ok, I think I am doing this wrong then, but really I don't know how the
> model is even supposed to look. We could do with a good rough doc on
> that I guess.
> 
> The quickstart does not make any model files beyond __init__.py, and
> webstart.py is setup by default to make tables from that file only. So
> how should the model work as per attaching the right mapper? And what is
> the user supposed to have to do to get quickstart setup-add to work
> right?
> 
> What I did ( wrong I'm sure ) is put in the __init__.py model:
> 
> from page import *
> 
> And in page.py:
> 
> DBSession = scoped_session(sessionmaker(autoflush=True,
> transactional=True))
> mapper = DBSession.mapper

That looks like begging for trouble... I think you should use the same
mapper instance on all models and the same DBSession.

> 
> But then that instantiation is in both model files. Looks wrong to me,
> but I couldn't figure out how to get the mapper I wanted available to
> the submodel file. Can I import DBSession from __init__? ( seemed like a
> circular dependancy, but I'm still amazed at what python can make sense
> of sometimes.

What I usually do when splitting my models in different modules is to do
all SA imports, scoped_session creation and mapper "instantiation" in
models/base.py and then in every model submoudle I do "from base import
*" to get all the symbols. It's in this base.py where I also define
custom TypeDecorators, a MappedClass base class every model subclass
derives from (with some useful methods like a SO-like set()) and any
other helper I want available at all model submodules.

In models/__init__.py I just import the external API from the model I
want available to the rest of the app to keep Demeter [1] happy and have
some model internal refactoring freedom (note that this avoids any
circular dependencies). Usually the only things I "export" to the
controllers are the DBSession and first-class business objects and *no*
SA querying functions (and_, or_, desc, etc..), I try my best to hide
queries inside classmethods so the controllers access a well-defined API
that makes sense to the app, this is to reduce coupling.

Alberto

[1] http://en.wikipedia.org/wiki/Law_of_Demeter

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears Trunk" 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/turbogears-trunk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to