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

查看:1479
本文介绍了在Django 1.5自定义用户模型中使用email作为用户名字段导致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:Local类'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/模型/#字段名称隐藏是不允许的

如何可以实现此目的? (除此之外,另外命名为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 还有一个用户名我没有必要为我

我决定创建我的自己的 AbstractUser

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

由子类化 AbstractBaseUser PermissionsMixin 我保留了大部分用户模型内置方法,而不添加任何代码。

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

我也利用这个机会创建一个自定义的管理器,以消除在 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 不能用于实际在所有现有约束下创建灵活的用户模型。

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 /开发/主题/认证/定制/#A-全EXA mple

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自定义用户模型中使用email作为用户名字段导致FieldError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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