How to make email field unique in model User from contrib.auth in Django

后端 未结 19 2057
夕颜
夕颜 2020-11-27 11:47

I need to patch the standard User model of contrib.auth by ensuring the email field entry is unique:

User._meta.fields[4].unique = True
<         


        
19条回答
  •  爱一瞬间的悲伤
    2020-11-27 11:54

    I went to \Lib\site-packages\django\contrib\auth\models and in class AbstractUser(AbstractBaseUser, PermissionsMixin): I changed email to be:

    email = models.EmailField(_('email address'), **unique=True**, blank=True)
    

    With this if you try to register with email address already present in the database you will get message: User with this Email address already exists.

提交回复
热议问题