Reputation: 154
Django allauth validates usernames for illegal characters and uniqueness based on case insensitivity when signing up or logging in. That's great. But I would like to use the same validation on my own profile form that has a OneToOne with User. As it stands, I can't get the same logic in my Profile form.
I thought it might be importing DefaultAccountAdapter
and overriding def clean_username
, but I get:
TypeError at /accounts/profile/
clean_username() takes exactly 2 arguments (1 given)
My forms.py
:
from allauth.account.adapter import DefaultAccountAdapter
from .profiles.models import Profile
class UserForm(forms.ModelForm):
class Meta:
model = get_user_model()
fields = ('username', 'first_name', 'last_name',)
widgets = {
'username': forms.TextInput(
attrs={
'autocapitalize': 'off',
'autocorrect': 'off',
'placeholder': 'Username'
}
),
'first_name': forms.TextInput(
attrs={
'autocapitalize': 'words',
'autocorrect': 'off',
'placeholder': 'First name'
}
),
'last_name': forms.TextInput(
attrs={
'autocapitalize': 'words',
'autocorrect': 'off',
'placeholder': 'Last name'
}
),
}
help_texts = {
'username': '',
}
def clean_username(self, username):
adapter = DefaultAccountAdapter()
username = adapter.clean_username(username)
return username
Upvotes: 0
Views: 1138
Reputation: 154
I figured it out eventually. Was overthinking it.
from allauth.account.adapter import get_adapter
#...
def clean_username(self):
value = self.cleaned_data['username']
value = get_adapter().clean_username(value)
return value
Upvotes: 1