2013-06-06 2 views

답변

27

주위를 파고 들자 마자

https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#custom-users-and-the-built-in-auth-forms

범인 django.contrib.auth.forms.py 내부 UserCreationForm 내부 clean_username 함수이다. 몇 티켓은 생성 된,하지만 분명히 메인테이너는 결함 생각하지 않습니다 :

https://code.djangoproject.com/ticket/20188

https://code.djangoproject.com/ticket/20086

def clean_username(self): 
     # Since User.username is unique, this check is redundant, 
     # but it sets a nicer error message than the ORM. See #13147. 
     username = self.cleaned_data["username"] 
     try: 
      User._default_manager.get(username=username) 
     except User.DoesNotExist: 
      return username 
     raise forms.ValidationError(self.error_messages['duplicate_username']) 

이 파일의 User 직접 내장 사용자 모델을 참조한다. 그것을 해결하기 위해

, 내 사용자 정의

from models import User #you can use get_user_model 
from django.contrib.auth.forms import UserCreationForm 
from django.contrib.auth.admin import UserAdmin 
from django.contrib.auth import forms 

class MyUserCreationForm(UserCreationForm): 
    def clean_username(self): 
     # Since User.username is unique, this check is redundant, 
     # but it sets a nicer error message than the ORM. See #13147. 
     username = self.cleaned_data["username"] 
     try: 
      User._default_manager.get(username=username) 
     except User.DoesNotExist: 
      return username 
     raise forms.ValidationError(self.error_messages['duplicate_username']) 

    class Meta(UserCreationForm.Meta): 
     model = User 

class MyUserAdmin(UserAdmin): 
    add_form = MyUserCreationForm 

admin.site.register(User,MyUserAdmin) 

을 형성 생성 아니면 User 변수를 대체하기 위해 원래의 UserCreationForm 패치 원숭이를 시도 할 수 있습니다.

+1

멋진 캐치. 이것은 Django 문서가 사용자 모델을 확장 할 때 실제로이 클래스들을 사용하기를 실제로 권장한다는 점을 고려하여 확실히 다루어 져야합니다. – dustinfarris

+1

내가 찾는 해결책. – Charlesliam

+1

그것은 내 하루를 구했다! – neelix

4

이것은 마이그레이션이 실행되지 않았기 때문입니다. 이 문제는 다음 명령을 실행하여 나를 위해 해결 :

파이썬 manage.py의 syncdb

3

장고 1.8

을하면 앱이 아직 contrib.auth으로도 문제가 될 수 다음이 마이그레이션을 사용하지 않는 그들을 사용합니다. 내 애플 리케이션을위한 마이 그 레이션을 사용하면 나를 위해 그것을 해결.

$ ./manage.py makemigrations <my_app> 
$ ./manage.py migrate 
+0

당신은 내 정신을 구했습니다. 분명히 마이그레이션을 지우는 경우 마이그레이션 폴더와'__init__ '을 유지해야합니다.py'를 입력하면 Django는 초기 마이그레이션을 생성하지 못하고 '마이그레이션'이 실패합니다. –

관련 문제