ここで、ユーザーマネージャー作成認証バックエンドを作成しますが、マネージャーでのログイン中にエラーが発生しました。次のようにAbstractBaseUserを使用してユーザーを作成します:
models.py
class Manager(AbstractBaseUser, PermissionsMixin):
managing_director = models.ForeignKey(ManagingDirector)
manager_name = models.CharField(max_length=50)
manager_email = models.CharField(max_length=50,null=True)
manager_mobile = models.CharField(max_length=50,null=True)
manager_address = models.TextField()
username = models.CharField(max_length=50,unique=True)
manager_status = models.BooleanField(default=True)
is_staff = models.BooleanField(default=True)
class Meta:
verbose_name = _('Manager')
verbose_name_plural = _('Managers')
def __unicode__(self):
return self.manager_name
def get_full_name(self):
return self.manager_name
def get_short_name(self):
return self.manager_name
マネージャーにログインを提供する必要があるため、提供するbackends.pyを作成します
backends.py
from staffs.models import Manager
class ManagerBackend:
# Create an authentication method
# This is called by the standard Django login procedure
def authenticate(self, username=None, password=None):
try:
# Try to find a user matching your username
user = Manager.objects.get(username=username)
# Check the password is the reverse of the username
if password == user.password:
# Yes? return the Django user object
return user
else:
# No? return None - triggers default login failed
return None
except Manager.DoesNotExist:
# No user was found, return None - triggers default login failed
return None
# Required for your backend to work properly - unchanged in most scenarios
def get_user(self, user_id):
try:
return Manager.objects.get(pk=user_id)
except Manager.DoesNotExist:
return None
settings.py の AUTHENTICATION_BACKENDS タプルに backends.mybackend.ManagerBackend を追加し、インストール済みアプリの settings.pyにスタッフを追加します。
...................
...................
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'backends.mybackend.ManagerBackend',
)
.................
.................
DJANGO_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
)
LOCAL_APPS = (
'staffs',
'products',
'distributions',
'subscriptions',
)
INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS
...............
...............
エラー
TypeError at /login/
save() got an unexpected keyword argument 'update_fields'
Request Method: POST
Request URL: http://127.0.0.1:8000/login/?next=/Manager/add/
Django Version: 1.5.1
Exception Type: TypeError
Exception Value:
save() got an unexpected keyword argument 'update_fields'
Exception Location: /home/node1/Office/projectenv/local/lib/python2.7/site-packages/django/contrib/auth/models.py in update_last_login, line 31
Python Executable: /home/node1/Office/projectenv/bin/python
Python Version: 2.7.3
Python Path:
['/home/node1/Office/projects.inzane/smedia/smedia',
'/home/node1/Office/projectenv/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
'/home/node1/Office/projectenv/local/lib/python2.7/site-packages/pip-1.3.1-py2.7.egg',
'/home/node1/Office/projectenv/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
'/home/node1/Office/projectenv/lib/python2.7/site-packages/pip-1.3.1-py2.7.egg',
'/home/node1/Office/projectenv/lib/python2.7',
'/home/node1/Office/projectenv/lib/python2.7/plat-linux2',
'/home/node1/Office/projectenv/lib/python2.7/lib-tk',
'/home/node1/Office/projectenv/lib/python2.7/lib-old',
'/home/node1/Office/projectenv/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/home/node1/Office/projectenv/local/lib/python2.7/site-packages',
'/home/node1/Office/projectenv/local/lib/python2.7/site-packages/PIL',
'/home/node1/Office/projectenv/lib/python2.7/site-packages',
'/home/node1/Office/projectenv/lib/python2.7/site-packages/PIL']
Server time: Sat, 13 Jul 2013 06:56:46 -0500
トレースバック
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/login/?next=/Manager/add/
Django Version: 1.5.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'staffs',
'products',
'distributions',
'subscriptions')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/home/node1/Office/projectenv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/home/node1/Office/projectenv/local/lib/python2.7/site-packages/django/views/decorators/debug.py" in sensitive_post_parameters_wrapper
69. return view(request, *args, **kwargs)
File "/home/node1/Office/projectenv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/home/node1/Office/projectenv/local/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
89. response = view_func(request, *args, **kwargs)
File "/home/node1/Office/projectenv/local/lib/python2.7/site-packages/django/contrib/auth/views.py" in login
46. auth_login(request, form.get_user())
File "/home/node1/Office/projectenv/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py" in login
95. user_logged_in.send(sender=user.__class__, request=request, user=user)
File "/home/node1/Office/projectenv/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py" in send
170. response = receiver(signal=self, sender=sender, **named)
File "/home/node1/Office/projectenv/local/lib/python2.7/site-packages/django/contrib/auth/models.py" in update_last_login
31. user.save(update_fields=['last_login'])
Exception Type: TypeError at /login/
Exception Value: save() got an unexpected keyword argument 'update_fields'