2014-03-13 4 views
0
슈퍼 클래스

의 구현하지 속성은 내 웹 사이트의 사용자를위한 모델을 만들었습니다. 사용자가 폼을 채운 후 레지스터를 클릭하면 폼을 검사하고 유효하면 사용자 데이터를 저장하는 다른 뷰를 만들었습니다.장고 서브 클래스 (모델)

def register_user(request): 
    if request.method == 'POST': 
     form = MyRegistrationForm(request.POST) 
     if form.is_valid(): 
      form.save() #save user registeration data 
      return HttpResponseRedirect('/accounts/register_success') 

    # in the first time it generate empty form: 
    args = {} 
    args.update(csrf(request)) 

    args['form'] = MyRegistrationForm() 

    return render_to_response('accounts/register.html', args) 

그리고 형태 : 여기

는 뷰의

class MyRegistrationForm(UserCreationForm): 
    email = forms.EmailField(required=True) 
    first_name = forms.CharField(required=False,max_length=30) 
    last_name = forms.CharField(required=False,max_length=30) 

    class Meta: 
     model = ImrayWebsiteUser 
     fields = ('username', 'email', 'password1', 'password2', 
        'first_name', 'last_name') 

    def save(self, commit=True): 
     user = super(MyRegistrationForm, self).save(commit=False) 
     user.email = self.cleaned_data['email'] 
     user.firstname = self.cleaned_data['first_name'] 
     user.lastname = self.cleaned_data['last_name'] 
     # user.set_password(self.cleaned_data['password1']) 

     if commit: 
      user.save() 

     return user 

내가 이렇게보기를 실행하고 전송 (양식 정보를 입력하고 등록을 클릭하면 문제는,입니다 양식에 대한 데이터) 에는 set_password이라는 속성이 없음을 나타내는 AttributeError이 있습니다. 내 사용자 ImrayWebsiteUser이 내장 된 사용자 유형을 명확하게 구현했기 때문에 이러한 일이 발생하지 않아야합니다.

AttributeError at /accounts/register/ 
'ImrayWebsiteUser' object has no attribute 'set_password' 
Request Method: POST 
Request URL: http://127.0.0.1:8000/accounts/register/ 
Django Version: 1.5 
Exception Type: AttributeError 
Exception Value:  
'ImrayWebsiteUser' object has no attribute 'set_password' 
Exception Location: C:\Python27\lib\site-packages\django\contrib\auth\forms.py in save, line 107 
Python Executable: C:\Python27\python.exe 
Python Version: 2.7.6 
Python Path:  
['C:\\Users\\Imray\\ImraySite\\bitbucket', 
'C:\\Python27\\lib\\site-packages\\setuptools-2.1-py2.7.egg', 
'C:\\Windows\\SYSTEM32\\python27.zip', 
'C:\\Python27\\DLLs', 
'C:\\Python27\\lib', 
'C:\\Python27\\lib\\plat-win', 
'C:\\Python27\\lib\\lib-tk', 
'C:\\Python27', 
'C:\\Python27\\lib\\site-packages'] 
Server time: Thu, 13 Mar 2014 11:56:27 +0200 

답변

1

귀하의 OnetoOneField는 사용자 모델의 구현되지 않습니다 :

는 오류 코드입니다.

https://docs.djangoproject.com/en/1.6/ref/models/fields/#ref-onetoone

당신은 귀하의 경우 object.user 속성으로 OneToOneField에 액세스 할 수 있습니다.

당신이 set_password를 구현 붙박이 UserCreationForm을 사용하고 있기 때문에, 당신은 set_password라는 모델의 방법을 만들고 self.user.set_password

를 호출하지만 사용자 정의 사용자 구현 들여다 추천 :

https://docs.djangoproject.com/en/1.6/topics/auth/customizing/#auth-custom-user

편집 : 이것은 단지 귀하의 질문에서 얻은 것에 따라 개념입니다 :

당신은 같은 것을 할 수 RegistrationForm에도

: 또한

class MyRegistrationForm(UserCreationForm): 
    ....your registration form attributes from UserProfile Model 
    class Meta: 
    fields = ('username', 'email', 'firstname', 'lastname') # fields of User Model not your UserProfile Model 

    def save(self, commit=True): 
    user = super(MyRegistrationForm, self).save(commit=commit) 
    userprofile = UserProfile(user=user) 
    userprofile.attribute = form.cleaned_data[attribute_name]... 
    return user|userprofile #as required 

csrf는 {%의 csrf_token의 %} 정말

+0

내 사용자 사이의 유일한 차이와 같은 템플릿에 이미 사용할 수있는 내장 사용자 유형 내 사용자가 내 프로젝트의 다른 모델과 관계가 있다는 것입니다. 다른 방법으로 내 사용자를 구현해야합니까? – CodyBugstein

+0

또한 나에게 말해 줄 수 있겠는가, 아니면 내가 '자기'가 실제로 무엇인지를 알 수있는 곳을 가르쳐 주겠습니까? (Platonic이 아닌, 어떤 객체인지 - 세션인가? 사용자인가? 일반적으로 무엇을 할 수 있는가?) – CodyBugstein

+0

'self'는 클래스가 초기화되는 객체 인자이다./전화 했어. 그것은 C++이나 java에서'this'와 같은 것입니다. 파이썬 세계에서'Explicit은 암묵적보다 낫다 '. 기본적으로 당신이 할 수있는 것은 inbuilt 사용자 모델에 대한 foreignkey를 생성하고 [사용자 모델 자체가 제공하지 않는] 추가 필드를 추가하는 것입니다. – sagarchalise