4

在这里,我创建了一个用户管理器创建身份验证后端,但是在使用管理器登录时出现错误我使用 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 来提供

后端.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'
4

1 回答 1

10

如您所见,错误是由该行引起的

user.save(update_fields=['last_login'])

方法从不接受关键字参数的类(通过)save继承。我可以向您建议任何解决方案,因为我不知道您需要的功能。但是,如果您不需要像我一样保存对 db 的任何更改,您可以简单地重写此方法以接受关键字参数并且什么也不做models.ModelAbstractBaseUser

def save (self, *args, **kwargs):
    """saving to DB disabled"""
    pass
于 2013-08-23T14:30:02.493 回答