我可以在 Django 1.5 中更改 USERNAME_FIELD 而不创建自定义用户吗? [英] Can I change the USERNAME_FIELD in Django 1.5 without creating a custom user?
问题描述
我正在尝试使用默认 Django 用户模型中的电子邮件字段作为用户名.我正在使用 Django 1.5,我看到默认用户有一个 USERNAME_FIELD
属性.
I am trying to use the email field in the default Django user model as the username. I am using Django 1.5 and I saw that the default user has a USERNAME_FIELD
property.
在我的项目中,我想将以下 USERNAME_FIELD = 'email'
设置为用户模型中的默认值.
In my project, I would like to set the following USERNAME_FIELD = 'email'
as a default in the user model.
这个小而根本的调整是我唯一想在用户模型中改变的地方.我想知道是否有一种方法可以更改 USERNAME_FIELD
而不必对 AbstractUser
进行子类化.我在 这个问题 中看到,您可以将 AbstractUser
子类化并为其编写自定义管理器.
This small but fundamental tweak is the only thing I would like to change in the user model. I was wondering if there is a way of changing the USERNAME_FIELD
without having to subclass the AbstractUser
. I saw in this question that you can subclass the AbstractUser
and write a custom manager for it.
所以我想知道是否有更简单的方法来更改该属性?
So I was wondering if there is a simpler way of changing that property?
如果没有,扩展 AbstractUser
以使用电子邮件字段作为用户名的最小方法是什么?
And if not, what would be the minimal way of extending the AbstractUser
to use the email field as username?
推荐答案
#Your app's __init__.py
from django.contrib.auth.models import User
User.USERNAME_FIELD = 'email'
这篇关于我可以在 Django 1.5 中更改 USERNAME_FIELD 而不创建自定义用户吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!