This is fairly ugly but you can probably monkeypatch the User objects
property, eg. in a middleware:
# manager.py
from django.contrib.auth.models import UserManager
class MyUserManager(UserManager):
def get_query_set(self):
qs = super(MyUserManager, self).get_query_set()
return qs.select_related('profile')
# middleware.py
from django.contrib.auth.middleware import AuthenticationMiddleware
from managers import MyUserManager
class MyAuthMiddleware(AuthenticationMiddleware):
def process_request(self, request):
super(AuthenticationMiddleware, self).process_request(request)
User.objects = MyUserManager()
return None
Then replace the line in settings.py
:
MIDDLEWARE_CLASSES = (
# ...
'django.contrib.auth.middleware.AuthenticationMiddleware',
# ...
)
By:
# settings.py
MIDDLEWARE_CLASSES = (
# ...
'yourapp.middleware.MyAuthMiddleware',
# ...
)
Note1: This code is purely theoric, never tested nor I have the time to.
Note2: I couldn't recommend using this solution from a long-term maintenability point of view.
Note3: If someone suggest something else, you should probably listen to him or her more than me.
Note4: As a probably better idea, why not trying to query for Profile, which is a model class you have total control on? You can always retrieve the user object from a profile anyway, so…