I am trying to store a session factory in the apps settings object,
following this: 
https://docs.pylonsproject.org/projects/pyramid_cookbook/dev/sqla.html#using-a-non-global-session

Below is my code, copied and pasted:

# request.py
from pyramid.request import Request
from pyramid.decorator import reify
class MyRequest(Request):
    @reify
    def db(self):
        maker = self.registry.settings['db.sessionmaker']
        return maker()

# __init__.py
from mainapp.request import MyRequest
from sqlalchemy.orm import sessionmaker

def main(global_config, **settings):
    config = Configurator(settings=settings)
    engine = engine_from_config(settings, prefix='sqlalchemy.')
    config.set_request_factory(MyRequest)
    maker = sessionmaker(bind=engine)
    settings['db.sessionmaker'] = maker

So the problem is when I do: print settings.items() in the "def
main()" function in __init__.py I get the following:

[('default_locale_name', 'en'), ('debug_routematch', 'false'),
('db.sessionmaker', <class 'sqlalchemy.orm.session.Session'>),
('sqlalchemy.url', 'mysql://root@localhost/mainapp'),
('debug_authorization', 'false'), ('debug_templates', 'true'),
('reload_templates', 'true'), ('mako.directories',
'mainapp:templates'), ('debug_notfound', 'false')]

Note: db.sessionmaker is present above.

Now when I do: print request.registry.settings.items() in my view
function, I get:
[('default_locale_name', 'en'), ('debug_routematch', False),
('reload_resources', False), ('sqlalchemy.url', 'mysql://
root@localhost/mainapp'), ('debug_authorization', False),
('debug_templates', True), ('reload_templates', True),
('reload_assets', False), ('mako.directories', 'mainapp:templates'),
('debug_notfound', False)]

Now note, db.sessionmaker is not present anymore. So where has it
gone? I thought I had registered it. I cannot access this session.
Everytime I try do request.db in my view I get "KeyError:
'db.sessionmaker' ". Obviously because it does not exist anymore.

Thanks for reading.

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" 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/pylons-discuss?hl=en.

Reply via email to