如何创建一个用户名只有名字,姓氏和emai。而不是使用用户名 [英] How to create a user with just First name, Last name and emai. And not with the username
问题描述
有没有办法创建没有用户名的用户?我尝试用first_name,last_name,密码和电子邮件。但是有一个错误:
Is there any way to create a user without the username? I tried doing this by first_name, last_name, password and email. But got an error:
TypeError at /accounts/register/
create_user() takes at least 2 arguments (3 given)
所以,我搜索了这样创建,然后发现django需要一个用户名。但我希望还有其他一些方法。任何人都可以引导我,如果有的话。谢谢。
So, I searched for creating so, and then found that django needs a username. But I hope there is some other way around. Can anyone please guide me through if there is. Thank you.
推荐答案
如果不想使用自定义用户模型,可以将 UserCreationForm
并覆盖用户名
字段:
If you don't want to use a custom user model you can subclass UserCreationForm
and override the username
field as such:
from django.contrib.auth.forms import UserCreationForm
class UserCreationForm(UserCreationForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'username',)
username = forms.EmailField(label='Email', max_length=255)
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.email = user.username
user.save()
return user
现在您有一个 UserCreationForm
验证并使用用户名的电子邮件地址,并自动将电子邮件字段设置为用户名,以使其保持同步。这不会设置您需要做的密码
字段。根据需要进行更改,希望能让您走。
Now you have a UserCreationForm
that will validate and use an email address for the username, in addition to setting the email field to the username automatically to keep them in sync. This doesn't set the password
field, which you would need to do as well. Change as you need, hope that gets you going.
这篇关于如何创建一个用户名只有名字,姓氏和emai。而不是使用用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!