SohpContext creates a Session, then loses it immediately.  That's your  
transactional context getting thrown away basically.  You should have  
a Session open for the lifespan of all ORM operations which includes  
lazy loads.   See 
http://www.sqlalchemy.org/docs/04/session.html#unitofwork_contextual_lifespan 
  for some more ideas on this.


On Jun 12, 2008, at 1:06 PM, J. Cliff Dyer wrote:

>
> In the following code, I am using django templates to render data  
> from a
> SQLAlchemy-mapped database.  I subclass django.template.Context, so  
> that
> I can pass it a unique ID, from which it determines what to pull from
> the DB.  But when it comes time to render the template (that is:  
> when I
> actually try to access data in the database), I get an
> UnboundExecutionError.  If I instantiate the Context object  
> directly, I
> don't have any problems.  Any idea why this would be, and how I can  
> get
> my class to work?  I'd rather keep the lazy loading semantics  
> intact, if
> possible.
>
> In the code below, render_1() has no problem, while render_2() raises
> the error.  Below the code, I show the output I'm getting.
>
> #### CODE ####
>  from django.conf import settings
> settings.configure()
>
> from django.template import Template, Context
>
> from sqlalchemy.orm.session import Session
> from cdla.orm import docsouth, docsouth_sohp
>
> class SohpContext(Context):
>    def __init__(self, sohp_id):
>        s = Session()
>        q = s.query(docsouth_sohp.Interview)
>        context_dict = {'interview':  
> q.filter_by(sohp_id=sohp_id).one()}
>        super(SohpContext, self).__init__(context_dict)
>
> def render_1(sohp_id):
>    print "render_1"
>    template = Template('''
> {% for p in interview.participants %}\
>  * {{ p.participant.participant_firstname }}
> {% endfor %}''')
>    s = Session()
>    c = Context({'interview':
> s.query(docsouth_sohp.Interview).filter_by(sohp_id=sohp_id).one()})
>    print template.render(c)
>
> def render_2(sohp_id):
>    print "render_2"
>    template = Template('''
> {% for p in interview.participants %}\
>  * {{ p.participant.participant_firstname }}
> {% endfor %}''')
>    c = SohpContext(sohp_id)
>    print template.render(c)
>
> if __name__ == '__main__':
>    render_1('A-0001')
>    render_2('A-0001')
> #### END CODE ####
>
> #### RESULTS ####
> $ python error_reduce.py
> /net/docsouth/dev/lib/python/sqlalchemy/logging.py:62: FutureWarning:
> hex()/oct() of negative int will return a signed string in Python 2.4
> and up
>  return "%s.%s.0x..%s" % (instance.__class__.__module__,
> render_1
>
>  * Richard
>  * Richard
>  * Jack
>  * Jack
>
> render_2
> Traceback (most recent call last):
>  File "error_reduce.py", line 37, in ?
>    render_2('A-0001')
>  File "error_reduce.py", line 33, in render_2
>    print template.render(c)
>  File "/usr/lib/python2.3/site-packages/django/template/__init__.py",
> line 168, in render
>    return self.nodelist.render(context)
>  File "/usr/lib/python2.3/site-packages/django/template/__init__.py",
> line 705, in render
>    bits.append(self.render_node(node, context))
>  File "/usr/lib/python2.3/site-packages/django/template/__init__.py",
> line 718, in render_node
>    return(node.render(context))
>  File
> "/usr/lib/python2.3/site-packages/django/template/defaulttags.py",  
> line
> 93, in render
>    values = self.sequence.resolve(context, True)
>  File "/usr/lib/python2.3/site-packages/django/template/__init__.py",
> line 563, in resolve
>    obj = resolve_variable(self.var, context)
>  File "/usr/lib/python2.3/site-packages/django/template/__init__.py",
> line 650, in resolve_variable
>    current = getattr(current, bits[0])
>  File "/net/docsouth/dev/lib/python/sqlalchemy/orm/attributes.py",  
> line
> 44, in __get__
>    return self.impl.get(instance._state)
>  File "/net/docsouth/dev/lib/python/sqlalchemy/orm/attributes.py",  
> line
> 279, in get
>    value = callable_()
>  File "/net/docsouth/dev/lib/python/sqlalchemy/orm/strategies.py",  
> line
> 432, in __call__
>    raise exceptions.UnboundExecutionError("Parent instance %s is not
> bound to a Session, and no contextual session is established; lazy  
> load
> operation of attribute '%s' cannot proceed" % (instance.__class__,
> self.key))
> sqlalchemy.exceptions.UnboundExecutionError: Parent instance <class
> 'cdla.orm.docsouth_sohp.Interview'> is not bound to a Session, and no
> contextual session is established; lazy load operation of attribute
> 'participants' cannot proceed
> $
> #### END RESULTS ####
>
>
> >


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