我在 Django 1.5 项目中有一个自定义用户,它使用该email
字段作为用户名:
class MyUser(AbstractUser):
my_custom_field = models.CharField(max_length=20, blank=True, null=True)
USERNAME_FIELD = 'email'
MyUser._meta.get_field_by_name('email')[0]._unique = True
MyUser.REQUIRED_FIELDS.remove('email')
如果我尝试像这样验证该用户:
auth_user = authenticate(username=email, password=password)
login(request, auth_user)
我明白了:
Traceback:
File "/Users/user/dev/proj/app/core/views.py" in post
39. login(request, auth_user)
File "/Users/user/.virtualenvs/proj/lib/python2.7/site-packages/django/contrib/auth/__init__.py" in login
92. request.session[BACKEND_SESSION_KEY] = user.backend
File "/Users/user/.virtualenvs/proj/lib/python2.7/site-packages/django/utils/functional.py" in inner
203. return func(self._wrapped, *args)
Exception Type: AttributeError at /signup
Exception Value: 'AnonymousUser' object has no attribute 'backend'
我应该如何验证自定义用户?