views:

558

answers:

2

Hi,

first of all: this is not the same as this. The ModelBackend has no request member.

I want to access the session of the current user without access to the global request object or his/her session ID.

Why? I wrote my own authentication backend, extending ModelBackend. In that I have the function get_user (self, user_id), that gets called with every request (this is done automatically by the Django auth middleware). Unfortunately, get_user doesn't have access to request, but nonetheless in my case I want to check with data in the session (Why again? Because I don't have a local database and get all data from a remote middleware. The session would be a comfortable way to do some kind of caching).

Cheers,

+1  A: 

The RemoteUserBackend (from django.contrib.auth.backends) uses special middleware, RemoteUserMiddleware, to access request data. Maybe you could do it this way, too.

drdaeman
+2  A: 

Ha, the answer is simple yet awful. Just add the request to your settings: In some middleware do

from django.conf import settings
settings.request = request

then you can access it lateron in any external module by

from django.conf import settings
request = settings.request

It feels wrong, but does what is requested. Through request you get access to the session.

If you know any 'more django-ish' way to do it, I'd be glad to hear it.

Boldewyn
Next submitted reply seems to be more django-ish through
Hotsyk