我创建了一个 AbstractUser 模型来向标准用户模型添加一些额外的字段。
我认为一切都设置正确,但是当我登录时,我收到此错误:
unbound method save() must be called with UserProfile instance as first argument (got nothing instead)
这是我的帐户/models.py
from django.db import models
from django.conf import settings
from django.contrib.auth.models import UserManager, AbstractUser
class UserProfile(AbstractUser):
mobile = models.CharField(max_length=20, blank=True)
homephone = models.CharField(max_length=20, blank=True)
updated = models.DateTimeField(blank=True, null=True)
objects = UserManager()
在我的设置中,我有:
AUTH_USER_MODEL = 'accounts.UserProfile'
我的身份验证中间件:
from django.conf import settings
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
from accounts.models import UserProfile
#Authentication Middleware using a external cookie named AUTHENTICATION
class CookieMiddleware(object):
def process_request(self, request):
#if not hasattr(request, 'userprofile'):
# raise ImproperlyConfigured()
if "AUTHENTICATION" not in request.COOKIES:
#Cookie not found - do nothing
return
#Token found - first check if the user is allready is logged in
if request.user.is_authenticated():
return
#Not logged in, then send to RemoteUserBackend.py
token = request.COOKIES["AUTHENTICATION"]
#Return if the cookie length is 0
if len(token) == 0:
return
UserProfile = authenticate(token=token)
request.UserProfile = UserProfile
if request.UserProfile:
login(request, request.UserProfile)
我的 RemoteUserBackend.py 用于登录外部用户:
from django.conf import settings
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
from base64 import b64decode
from hashlib import sha1
from urllib import unquote
from sitetasks import tasks
from accounts.models import UserProfile
class Backend( object ):
def authenticate(self, username=None, password=None, token=None):
#Unescape token
unescaped_token = unquote(token)
#Decode token
decoded_token = unescaped_token.decode('base64')
#Split the token into tree variable
secret, hashstring, userID = decoded_token.split('-', 2)
#Secret needs to bee in lower to match shared secret
secret_lower = secret.lower()
#Make string of SHARED_SECRET, hashstring, userID
check_string = "%s%s%s" % (settings.SHARED_SECRET, hashstring, userID)
#sha1 the string
sha1_check_string = sha1(check_string)
#Check if the SHARED_SECRET is matching cookie secret
cookie_valid = sha1_check_string.hexdigest() == secret_lower
if cookie_valid:
try:
userprofile = UserProfile.objects.get(username=userID)
#The user exist, then update the user
#Make celery worker update user asynchronous
tasks.user_update.delay(user_id=userID)
except UserProfile.DoesNotExist:
# Create a new user
userprofile = UserProfile(username=userID)
userprofile.is_staff = False
userprofile.is_superuser = False
userprofile.save() #Save the user
#Make celery worker update user asynchronous
tasks.user_update.delay(user_id=userID)
return UserProfile
return None
def get_user(self, user_id):
try:
return UserProfile.objects.get(pk=user_id)
except UserProfile.DoesNotExist:
return None
我得到的错误的回溯是这样的:
Request Method: GET
Request URL: http://mydomain.com/
Django Version: 1.6.dev20130302084542
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
'django.contrib.admindocs',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.flatpages',
'south',
'djcelery',
'gunicorn',
'sorl.thumbnail',
'template_utils',
'compressor',
'tagging',
'ckeditor',
'debug_toolbar',
'mptt',
'accounts',
)
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'myproj.CookieMiddleware.CookieMiddleware')
Traceback:
File "/home/USER/.virtualenvs/SITE/downloads/django-trunk/django/core/handlers/base.py" in get_response
82. response = middleware_method(request)
File "/home/USER/.virtualenvs/SITE/myproj/myproj/CookieMiddleware.py" in process_request
35. login(request, request.UserProfile)
File "/home/USER/.virtualenvs/SITE/downloads/django-trunk/django/contrib/auth/__init__.py" in login
86. user_logged_in.send(sender=user.__class__, request=request, user=user)
File "/home/USER/.virtualenvs/SITE/downloads/django-trunk/django/dispatch/dispatcher.py" in send
182. response = receiver(signal=self, sender=sender, **named)
File "/home/USER/.virtualenvs/SITE/downloads/django-trunk/django/contrib/auth/models.py" in update_last_login
31. user.save(update_fields=['last_login'])
Exception Type: TypeError at /
Exception Value: unbound method save() must be called with UserProfile instance as first argument (got nothing instead)
有人看到我在这里做错了什么吗?