使用电子邮件ID作为唯一键的自定义用户模型的Django身份验证 [英] Django authentication with custom user model with email-id as unique key

查看:1583
本文介绍了使用电子邮件ID作为唯一键的自定义用户模型的Django身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在电子邮件ID上实现独特的约束(不重复默认用户模型字段)我从django执行了

 。 contrib.auth.models import AbstractUser 
class User(AbstractUser):
#一些额外的字段
class Meta:
unique_together =('email',)

然后在设置文件 AUTH_USER_MODEL ='myApp.User' p>

mysql表显示了列(用户名和电子邮件)作为UNI密钥。



到目前为止看起来不错,但是当它来了认证我不知道我在犯错误。但它不工作/登录



此代码适用于用户名是唯一的密钥,我们通过用户名而不是电子邮件登录。



任何帮助/潜在客户将不胜感激。



编辑:我必须编写自定义的modelBackend(在这里解释 http://scottbarnham.com/blog/2008/08/21/extending -the-django-user-model-with-inheritance /
但不知道如何,我在这里看到很多代码 https://github.com/django/django/blob/master/django/contrib/auth/ init .py

解决方案

您需要定义backends.py,如下所示:


$导入用户,check_password

class EmailAuthBackend(object):
$
$ $ $ $ $ b电子邮件身份验证后端

允许用户使用电子邮件/密码对而不是
登录用户名/密码对。


def authenticate(self,username = None,password = None):
验证基于用户名的电子邮件地址的用户。 $
尝试:
user = User.objects.get(email = username)
如果user.check_password(密码):
返回用户
除了用户。 NoNotExist:
return None

def get_user(self,user_id):
从user_id获取一个User对象。
try:
return User.objects.get(pk = user_id)
除了User.DoesNotExist:
返回无

在您的设置文件中添加以下内容:

  AUTHENTICATION_BACKENDS =('backends.EmailAuthBackend',)

这将启用使用电子邮件登录。 >

有关详细信息,请访问


to achieve unique constraint (without repetition of default user models fields) on email-id I did this

from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
    # some extra fields
    class Meta:
        unique_together = ('email', )

and then in settings file AUTH_USER_MODEL = 'myApp.User'

mysql table shows both column(username and email) as UNI key.

so far looks good but when it comes to authentication I'm not sure where I'm making mistake. but its not working/loggin-in

this code works when username is the unique key and we login through username instead of email.

any help/leads would be appreciated.

EDIT: do i have to write custom modelBackend (explained here http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/) but not sure how, I see lots of code here https://github.com/django/django/blob/master/django/contrib/auth/init.py

解决方案

You need to define backends.py like this:

from django.contrib.auth.models import User, check_password

class EmailAuthBackend(object):
"""
Email Authentication Backend

Allows a user to sign in using an email/password pair rather than
a username/password pair.
"""

def authenticate(self, username=None, password=None):
    """ Authenticate a user based on email address as the user name. """
    try:
        user = User.objects.get(email=username)
        if user.check_password(password):
            return user
    except User.DoesNotExist:
        return None 

def get_user(self, user_id):
    """ Get a User object from the user_id. """
    try:
        return User.objects.get(pk=user_id)
    except User.DoesNotExist:
        return None

And in your settings file add this:

AUTHENTICATION_BACKENDS = ('backends.EmailAuthBackend',)

This will enable the login with email.

For more details visit this.

这篇关于使用电子邮件ID作为唯一键的自定义用户模型的Django身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆