Python 如何在自定义注册表单中使用django allauth生成唯一用户名?

Python 如何在自定义注册表单中使用django allauth生成唯一用户名?,python,django,django-allauth,Python,Django,Django Allauth,我正在尝试从用户的名字生成一个唯一的用户名,因为它没有在注册表单中使用 我不确定如何在forms.py模块中实现allauth.account.adapter.DefaultAccountAdapter。请举个例子 from allauth.account.forms import SignupForm, LoginForm from allauth.account.adapter import get_adapter class CustomSignupForm(SignupForm): f

我正在尝试从用户的名字生成一个唯一的用户名,因为它没有在注册表单中使用

我不确定如何在forms.py模块中实现allauth.account.adapter.DefaultAccountAdapter。请举个例子

from allauth.account.forms import SignupForm, LoginForm
from allauth.account.adapter import get_adapter

class CustomSignupForm(SignupForm):
first_name = forms.CharField(max_length=30, label='First Name', widget=forms.TextInput(
attrs={'placeholder': 'First name', 'class': 'form-control'}))
last_name = forms.CharField(max_length=30, label='Last Name', widget=forms.TextInput(
attrs={'placeholder': 'Last name', 'class': 'form-control'}))

def __init__(self, *args, **kwargs):
    super(CustomSignupForm, self).__init__(*args, **kwargs)

    self.fields['email'].widget.attrs.update({'class': 'form-control'})
    self.fields['password1'].widget.attrs.update({'class': 'form-control'})
    self.fields['password2'].widget.attrs.update({'class': 'form-control'})

def signup(self, request, user):
    adapter = get_adapter()
    username = adapter.generate_unique_username(self, self.cleaned_data['first_name'])
    user.username = username
    user.first_name = self.cleaned_data['first_name']
    user.last_name = self.cleaned_data['last_name']
    user.save()