django_session not in "default" DB

109 views
Skip to first unread message

Jocelo

unread,
Jul 29, 2010, 4:03:24 PM7/29/10
to Django users
Hi,

I have two DB configured on my django project... one in oracle and the
other one in MySQL...
My Oracle DB is the default one, and on MySQL I store data that is not
really related to my main application.

I want to store session data in MySQL, since oracle is legacy but
whenever I try to store something on my session object
("request.session['temp']=1"), django looks for django_session table
on Oracle, crashing completely my app.

I have read something about dabatase routing, but I'm not sure if this
is the way to go for this.

How can I make django look for django_session's table on my
"secondary" MySQL DB ?

thanks.

Dennis Kaarsemaker

unread,
Jul 29, 2010, 4:39:20 PM7/29/10
to django...@googlegroups.com
On do, 2010-07-29 at 13:03 -0700, Jocelo wrote:
> Hi,
>
> I have two DB configured on my django project... one in oracle and the
> other one in MySQL...
> My Oracle DB is the default one, and on MySQL I store data that is not
> really related to my main application.
>
> I want to store session data in MySQL, since oracle is legacy but
> whenever I try to store something on my session object
> ("request.session['temp']=1"), django looks for django_session table
> on Oracle, crashing completely my app.
>
> I have read something about dabatase routing, but I'm not sure if this
> is the way to go for this.

It is the way to go.

> How can I make django look for django_session's table on my
> "secondary" MySQL DB ?

http://docs.djangoproject.com/en/dev/topics/db/multi-db/#automatic-database-routing has all the details. your router would look like:

class MyRouter(object):
def db_for_read(model, **hints):
if model == Session:
return 'secondary'
db_for_write = db_for_read


--
Dennis K.

They've gone to plaid!

Jocelo

unread,
Jul 30, 2010, 1:03:24 PM7/30/10
to Django users
Hi, Thanks for your previous reply. I'm still having problems... now
I'm getting the following error:

NameError: global name 'Session' is not defined

I check up the documentation, I understand that
'django.contrib.sessions.backends.db' makes the Session model
available in the DB router .... so, just by setting the
'django.contrib.session' in my INSTALLED_APPS is enough ? What am I
missing ?

Thanks again.

On Jul 29, 3:39 pm, Dennis Kaarsemaker <den...@kaarsemaker.net> wrote:
> On do, 2010-07-29 at 13:03 -0700, Jocelo wrote:
>
> > Hi,
>
> > I have two DB configured on my django project... one in oracle and the
> > other one in MySQL...
> > My Oracle DB is the default one, and on MySQL I store data that is not
> > really related to my main application.
>
> > I want to store session data in MySQL, since oracle is legacy but
> > whenever I try to store something on my session object
> > ("request.session['temp']=1"), django looks for django_session table
> > on Oracle, crashing completely my app.
>
> > I have read something about dabatase routing, but I'm not sure if this
> > is the way to go for this.
>
> It is the way to go.
>
> > How can I make django look for django_session's table on my
> > "secondary" MySQL  DB ?
>
> http://docs.djangoproject.com/en/dev/topics/db/multi-db/#automatic-da...has all the details. your router would look like:

Steve Holden

unread,
Aug 2, 2010, 1:56:18 PM8/2/10
to django...@googlegroups.com, Jocelo
That was a simple typo in Dennis' reply. Use \

if model == 'Session'

instead and that error will disappear.

regards
Steve

Reply all
Reply to author
Forward
0 new messages