I've a custom user model and an authentication backend. When I'm using shell the authentication function works fine but when it comes to Admin site login, it is just not working.
The code comes from Official Django docs
settings.py
# Authentication
AUTH_USER_MODEL = 'BBUser.UserInfo'
AUTHENTICATION_BACKENDS = [
'BBUser.models.AuthBackend',
'BBUser.models.AdminAuthBackend',
]
models.py
class UserInfo(AbstractBaseUser, PermissionsMixin):
openid = models.CharField(max_length=50, null=False, unique=True)
nickname = models.CharField(max_length=30, null=True)
...
USERNAME_FIELD = 'openid'
REQUIRED_FIELDS = []
...
objects = UserManager()
class AdminAuthBackend(object):
def authenticate(self, openid=None, password=None):
print "custom auth"
login_valid = ('100' == openid)
pwd_valid = ('tobeno.2' == password)
if login_valid and pwd_valid:
try:
user = UserInfo.objects.get(openid=openid)
except UserInfo.DoesNotExist:
user = UserInfo(openid=openid, password='get from .py')
user.is_staff = True
user.is_superuser = True
user.save()
return user
return None
def get_user(self, user_id):
try:
return UserInfo.objects.get(pk=user_id)
except UserInfo.DoesNotExist:
return None
As I've put a print in AdminAuthBackend.authenticate(), so I know when it is executed.
When I'm invoking authenticate() from shell, the custom auth method is invoked while in admin page, it is not.
Any help is appreciated.