gpt4 book ai didi

python - 使用Django添加用户时自动生成用户名

转载 作者:行者123 更新时间:2023-12-03 15:37:24 49 4
gpt4 key购买 nike

我试图从django管理员添加用户时将用户名另存为名字。目前,它已将None保存在用户名字段中,因为我已在自定义模型中排除了username

admin.py--

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from .models import UserProfile
from .forms import SignUpForm

class ProfileInline(admin.StackedInline):
model = UserProfile
can_delete = False
verbose_name_plural = 'Profile'
fk_name = 'user'


class CustomUserAdmin(UserAdmin):
inlines = (ProfileInline, )
list_display = ('email', 'first_name', 'last_name', 'is_staff')
list_select_related = ( 'profile', )

exclude = ('username',)

fieldsets = (
('Personal information', {'fields': ('first_name', 'last_name', 'email', 'password')}),
('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}),
('Important dates', {'fields': ('last_login', 'date_joined')}),
)

add_fieldsets = (
('None', {
'classes': ('wide',),
'fields': ('first_name','last_name', 'email', 'password1', 'password2')}
),
)


def get_inline_instances(self, request, obj=None):
if not obj:
return list()
return super(CustomUserAdmin, self).get_inline_instances(request, obj)


admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)

表格
from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Div, Field
from ajax_select.fields import AutoCompleteSelectField, AutoCompleteField
from phonenumber_field.formfields import PhoneNumberField
from . import models
from captcha.fields import ReCaptchaField


class SignUpForm(forms.Form):
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
phone_number = PhoneNumberField(label=_("Phone (Please state your country code eg. +44)"))
organisation = forms.CharField(max_length=50)
email = forms.EmailField()
password1 = forms.CharField(max_length=20)
password2 = forms.CharField(max_length=20)
captcha = ReCaptchaField(attrs={'theme' : 'clean'})



def signup(self, request, user):
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
"""
profile, created = models.UserProfile.objects.get_or_create(user=user)
profile.phone_number = self.cleaned_data['phone_number']
profile.organisation = self.cleaned_data['organisation']
profile.save()
user.save()
"""
up = user.profile
up.phone_number = self.cleaned_data['phone_number']
up.organisation = self.cleaned_data['organisation']
user.save()
up.save()

models.py-
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from easy_thumbnails.fields import ThumbnailerImageField
from ciasroot.settings import THUMBNAILER_SIZES, UPLOAD_PATH
from ciasroot.constants import GENDERS, LANGUAGES
from ciasroot.util import HashedPk
from phonenumber_field.modelfields import PhoneNumberField
import math, decimal, datetime, os
import uuid

def random_username(sender, instance, **kwargs):
if not instance.username:
instance.username = uuid.uuid4().hex[:30]
models.signals.pre_save.connect(random_username, sender=User)

class UserProfile(models.Model, HashedPk):
user = models.OneToOneField(User, unique=True, related_name ='profile')
job_title = models.CharField(max_length=128, blank=True, null=False, default="")
website = models.URLField(max_length=255, blank=True, null=True)
organisation = models.CharField(max_length=50, blank=True, null=True, default="")
phone_number = PhoneNumberField( blank=True, null=True)

def __str__(self):
return self.user.get_full_name()

def save(self, *args, **kwargs):
super(UserProfile, self).save(*args, **kwargs)
LookupSuggest.add("job_title", self.job_title)

我该如何插入用户名作为名字并创建自定义字段,即“必填”电子邮件。现在,password1和password2字段是必填字段。

任何帮助/链接都将受到高度赞赏。

最佳答案

要使用用户的名字自动填充用户名,您应该使用signal-将以下内容添加到已定义UserProfile的models.py中:

def set_username(sender, instance, **kwargs):
if not instance.username:
instance.username = instance.first_name
models.signals.pre_save.connect(set_username, sender=User)

这样做的问题是,如果您有两个具有相同名字的用户,则用户名将不是唯一的,因此您将从数据库中收到完整性错误。您可以检查唯一性并附加一个数字,直到获得唯一值:
def set_username(sender, instance, **kwargs):
if not instance.username:
username = instance.first_name
counter = 1
while User.objects.filter(username=username):
username = instance.first_name + str(counter)
counter += 1
instance.username = username
models.signals.pre_save.connect(set_username, sender=User)

或者,如果您根本不使用用户名,则可以使用 uuid将其设置为随机的唯一值:
import uuid

def random_username(sender, instance, **kwargs):
if not instance.username:
instance.username = uuid.uuid4().hex[:30]
models.signals.pre_save.connect(random_username, sender=User)

如果您打算使用电子邮件登录而不是使用用户名登录,则还需要实现电子邮件唯一性,将电子邮件添加到管理员用户创建表单中-这应该可以满足您的要求: https://gist.github.com/gregplaysguitar/1184995

关于python - 使用Django添加用户时自动生成用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45621300/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com