Multiple User subclasses custom Auth backend

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User
from model_utils.managers import InheritanceQuerySet


class CustomUserModelBackend(ModelBackend):
    def get_user(self, user_id):
        try:
            return InheritanceQuerySet(User).select_subclasses().get(pk=user_id)
        except User.DoesNotExist:
            return None

More like this

  1. convenience parent class for UserProfile model by willhardy 4 years ago
  2. ModelMixin by eallik 2 years, 6 months ago
  3. Access transparently the user profile information by gsakkis 2 years, 8 months ago
  4. Prefill ForeignKey caches by insin 4 years, 7 months ago
  5. Improved User Admin by gregb 3 years ago

Comments

rodested (on October 11, 2011):

Hello,

I'd really like to use it. How should I deploy it?

Thanks in anticipation.

#

ungenio41 (on October 12, 2011):

Well, first you put the above code in a backends.py file.

Then, in your settings.py, put the following:

AUTHENTICATION_BACKENDS = ('backends.CustomUserModelBackend',)

Then you can do something like this in one of your models.py:

from django.contrib.auth.models import User
from django.db import models

class MyUser(User):
    picture = models.ImageField()

#

(Forgotten your password?)