2014-06-12 1 views
0

Django allauth는 가입시 또는 로그인 할 때 대소 문자를 구분하지 않고 불법 문자 및 고유성에 대한 사용자 이름의 유효성을 검사합니다. 그러나 사용자와 함께 OneToOne이있는 내 프로필 양식에서 동일한 유효성 검사를 사용하고 싶습니다. 그대로, 내 프로필 양식에서 동일한 논리를 얻을 수 없습니다. Django allauth 어댑터로 사용자 이름을 정리하는 방법은 무엇입니까?

가 나는 DefaultAccountAdapter 및 최우선 def clean_username을 가져올 수 있습니다 생각하지만 난 얻을 :
TypeError at /accounts/profile/ 
clean_username() takes exactly 2 arguments (1 given) 

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 

답변

1

. 그것을 overthinking했다.

from allauth.account.adapter import get_adapter 

#... 

    def clean_username(self): 
     value = self.cleaned_data['username'] 
     value = get_adapter().clean_username(value) 
     return value 
관련 문제