开发者

Can I use email addresses as usernames in django-auth?

I'd like to use email addresses as a pseudo-username of sorts while using Django's auth module. Can I do this? User.username is limited to 30 characters which really isn't acceptable for lo开发者_如何学编程nger email addresses. I know that the User object also has an email_address property, but I'm not sure that this is for authentication. The problem is that username is required, but is too short for my application to be usable. For my application, a pseudonym such as a username doesn't make any sense, so I don't know how I can make this work. Is there a way to do what I'm talking about?


Generate the username programatically and subclass the auth backend to support emails:

from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User


class CustomAuthBackend(ModelBackend):
    def authenticate(self, email=None, password=None, username=None,
                     *args, **kwargs):
        try:
            user = User.objects.get(email=email)
            if user.check_password(password):
                return user
        except User.DoesNotExist:
            pass
        # fallback to login + password
        return super(CustomAuthBackend, self).authenticate(username=username,
                                                           password=password,
                                                           *args, **kwargs)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜