Is there a terser way to get a reference to my DB than self.request.root.db?

67 views
Skip to first unread message

Zak

unread,
Aug 11, 2012, 2:03:24 AM8/11/12
to pylons-...@googlegroups.com
self.request.root.db is quite a bit of dot notation and I'm wondering if there is a shortcut or better way to get db.

Malthe Borch

unread,
Aug 11, 2012, 3:08:18 AM8/11/12
to pylons-...@googlegroups.com
You could write a base class that defined it as a `property`.

On 11 August 2012 08:03, Zak <zakd...@gmail.com> wrote:
> self.request.root.db is quite a bit of dot notation and I'm wondering if
> there is a shortcut or better way to get db.
>
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/pylons-discuss/-/RwOSeHfYa3IJ.
> To post to this group, send email to pylons-...@googlegroups.com.
> To unsubscribe from this group, send email to
> pylons-discus...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pylons-discuss?hl=en.

Jonathan Vanasco

unread,
Aug 11, 2012, 5:07:34 PM8/11/12
to pylons-discuss
storing the db connection somewhere under the active request is the
optimal thing to do -- it makes the db generally accessible throughout
the app.

there are some ways to make it easier to get to:
- if you use a class based approach to writing views, you could have a
base class that does much of the work
- you could write a helper function


class base_class(object):
def dbSession(self):
return self.request.root.db

def getDbSession(request=None):
if not request:
request= get_current_request():
return request.root.db



kusut

unread,
Aug 12, 2012, 12:10:59 AM8/12/12
to pylons-...@googlegroups.com
On 12 August 2012 04:07, Jonathan Vanasco <jona...@findmeon.com> wrote:
> there are some ways to make it easier to get to:
> - if you use a class based approach to writing views, you could have a
> base class that does much of the work
> - you could write a helper function

- pyramid.config.Configurator.set_request_property [0]
- pyramid.request.Request.set_property [1]

[0] http://pyramid.readthedocs.org/en/latest/api/config.html#pyramid.config.Configurator.set_request_property
[1] http://pyramid.readthedocs.org/en/latest/api/request.html#pyramid.request.Request.set_property

--
http://kusut.web.id
Reply all
Reply to author
Forward
0 new messages