Using email as username field in Django 1.5 custom User model results in FieldError

前端 未结 4 1669
你的背包
你的背包 2020-12-15 05:06

I want to use an email field as the username field for my custom user model. I have the following custom User model subclassing Django\'s AbstractUser model:



        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 05:49

    You can edit your CustomUser to change the email field attribute to unique=True.

    Add this to the end of your custom user class like so:

    class CustomUser(AbstractUser):
        ...
        USERNAME_FIELD = 'email'
        ...
    CustomUser._meta.get_field_by_name('email')[0]._unique=True
    

    Note that we're changing _unique and not unique because the latter is a simple @property.

    This is a hack, and I would love to hear any "official" answers to resolve this.

提交回复
热议问题