Skip to content

Instantly share code, notes, and snippets.

@jarcoal
Created September 9, 2012 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarcoal/3681395 to your computer and use it in GitHub Desktop.
Save jarcoal/3681395 to your computer and use it in GitHub Desktop.
Simple Extension to Django's default ModelBackend to force it to select a profile in addition to the user.
AUTHENTICATION_BACKENDS = ('project_name.wherever.you.put.it.SmartModelBackend',)
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User
class SmartModelBackend(ModelBackend):
def get_user(self, user_id):
try:
#note that 'profile' needs to be set to whatever your profile's related_name is.
return User.objects.select_related('profile').get(pk=user_id)
except User.DoesNotExist:
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment