﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
32181	ModelBackend used hardcoded global variable UserModel that cannot be overrided.	Maxim Danilov	nobody	"It is not possible override Usermodel for custombackend which depends on ModelBackend.

if i want to change something un Usermodel class i should override whole authenticate method. it is not difficult but stupid and brakes SOLID rule

it's easy to change:
1. add **usermodel=UserModel** in ModelBackend class definitions
2. Change global variable UserModel to local **self.usermodel**

{{{

class ModelBackend(BaseBackend):
    """"""
    Authenticates against settings.AUTH_USER_MODEL.
    """"""
    usermodel=UserModel

    def authenticate(self, request, username=None, password=None, **kwargs):
        if username is None:
            username = kwargs.get(self.usermodel.USERNAME_FIELD)
        if username is None or password is None:
            return
        try:
            user = self.usersermodel._default_manager.get_by_natural_key(username)
        except UserModel.DoesNotExist:
            # Run the default password hasher once to reduce the timing
            # difference between an existing and a nonexistent user (#20760).
            self.usermodel().set_password(password)
        else:
            if user.check_password(password) and self.user_can_authenticate(user):
                return user

}}}

after that it can be possible to do something like that:


{{{
class LoginBackend(ModelBackend):
    """"""docstring for LoginBackend.""""""

    def authenticate(self, *args, **kwargs):
         self.usermodel = change_something_in(self.usermodel, *args, **kwargs)
         return super().authenticate(self, *args, **kwargs).
}}}


"	New feature	closed	contrib.auth	3.1	Normal	wontfix	custombackend, ModelBackend		Unreviewed	0	0	0	0	0	0
