在 Django 1.5 自定义用户模型中使用电子邮件作为用户名字段会导致 FieldError [英] Using email as username field in Django 1.5 custom User model results in FieldError

查看:29
本文介绍了在 Django 1.5 自定义用户模型中使用电子邮件作为用户名字段会导致 FieldError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用电子邮件字段作为我的自定义用户模型的用户名字段.我有以下自定义用户模型子类化 Django 的 AbstractUser 模型:

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:

class CustomUser(AbstractUser):
    ....
    email = models.EmailField(max_length=255, unique=True)

    USERNAME_FIELD = 'email'

但是当我跑步时

python manage.py sql myapp

python manage.py sql myapp

我收到以下错误:

FieldError:类CustomUser"中的本地字段email"与来自基类AbstractUser"的类似名称的字段发生冲突

FieldError: Local field 'email' in class 'CustomUser' clashes with field of similar name from base class 'AbstractUser'

我首先包含我自己的电子邮件字段的原因是向其中添加 unique=True 选项.否则我得到:

The reason I include my own email field in the first place is to add the unique=True option to it. otherwise I get:

myapp.customuser:USERNAME_FIELD 必须是唯一的.将 unique=True 添加到字段参数中.

myapp.customuser: The USERNAME_FIELD must be unique. Add unique=True to the field parameters.

现在,关于这个:https://docs.djangoproject.com/en/1.5/topics/db/models/#field-name-hiding-is-not-permitted
我如何可以实现这一目标?(然后将字段命名为user_email"或类似的名称)

Now, in respect to this: https://docs.djangoproject.com/en/1.5/topics/db/models/#field-name-hiding-is-not-permitted
How can I achieve this? (other then naming the field "user_email" or something like that instead)

推荐答案

Ian,非常感谢你的聪明回复 :)

Ian, thank you very much for the clever response :)

但是,我已经修补"了一个解决方案.

However, I've already "patched" me a solution.

因为 AbstractUser 也有一个 username 字段,这对我来说完全没有必要
我决定创建我的自己的"AbstractUser.

Since AbstractUser also have a username field which is totaly unnecessary for me
I decided to create my "own" AbstractUser.

通过继承 AbstractBaseUserPermissionsMixin 我保留了大部分 User 模型的内置方法,没有添加任何代码.

By subclassing AbstractBaseUser and PermissionsMixin I retain most of the User model built-in methods without adding any code.

我还利用这个机会创建了一个自定义 Manager 以消除在 username 字段中的使用:

I also took advantage of that opportunity to create a custom Manager to eliminate the use in username field all together:

from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager

class CustomUser(AbstractBaseUser, PermissionsMixin):
     ....
     email = models.EmailField(max_length=255, unique=True)
     first_name = ...
     last_name = ...
     is_active = ...
     is_staff = ...
     ....

     objects = CustomUserManager()

     USERNAME_FIELD = 'email'


class CustomUserManager(BaseUserManager):
     def create_user(self, email, password=None, **extra_fields):
          .....

     def create_superuser(self, email, password, **extra_fields):
          .....

此解决方案确实会导致重复 Django 的一些内置代码(主要是 AbstractUser 中已经存在的模型字段,例如 'first_name'、'last_name' 等),但也导致了更清洁用户对象和数据库表.

This solution does result in repetition of some of Django's built-in code (mainly model fields that already exist in AbstractUser such as 'first_name', 'last_name' etc.) but also in a cleaner User object and database table.

遗憾的是,1.5 中引入的带有 USERNAME_FIELD 的 flexibily 不能用于实际在所有现有约束下创建灵活的 User 模型.

It is a real shame that the flexibily introduced in 1.5 with USERNAME_FIELD can not be used to actualy create a flexible User model under all existing constrains.

官方文档中有一个全面的工作示例:https://docs.djangoproject.com/en/dev/topics/auth/customizing/#a-full-example

There is a comprehensive worked example available in the official docs: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#a-full-example

这篇关于在 Django 1.5 自定义用户模型中使用电子邮件作为用户名字段会导致 FieldError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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