问题描述
我尝试使用默认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而不创建自定义用户吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!