2015-01-25 4 views
0

생일과 오늘 날짜를 사용하여 내 사이트 사용자가 성인인지 확인하려고합니다. 장고에서 성인용인지 확인

나는 adult라는 내 사용자의 부울 속성을하고 난 다음과 같은 방법으로 설정하려고

from datetime import date 

... 

def set_adult(self): 
    today = date.today() 
    age = today.year - self.date_of_birth.year - ((today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day)) 
    if age > 18: 
     self.adult = True 
    else: 
     self.adult = False 

사용자를 등록 할 때, 나는 생일을 추가하기위한 필드가

class MyRegistrationForm(forms.ModelForm): 
    """ 
    Form for registering a new account. 
    """ 
    GENDER_CHOICES = (
     ('M', 'Male'), 
     ('F', 'Female'), 
    ) 
    email = forms.EmailField(widget=forms.EmailInput,label="Email") 
    date_of_birth = forms.DateField(widget=forms.DateInput(format='%m/%d/%Y'), label="Date of birth (MM/DD/YYYY)") 
    gender = forms.ChoiceField(widget=RadioSelect, choices=GENDER_CHOICES) 
    password1 = forms.CharField(widget=forms.PasswordInput, 
           label="Password") 
    password2 = forms.CharField(widget=forms.PasswordInput, 
           label="Password (again)") 

    class Meta: 
     model = MyUser 
     fields = ['email', 'date_of_birth', 'gender', 'password1', 'password2'] 

    def clean(self): 
     """ 
     Verifies that the values entered into the password fields match 

     NOTE: Errors here will appear in ``non_field_errors()`` because it applies to more than one field. 
     """ 
     cleaned_data = super(MyRegistrationForm, self).clean() 
     if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data: 
      if self.cleaned_data['password1'] != self.cleaned_data['password2']: 
       raise forms.ValidationError("Passwords don't match. Please enter both fields again.") 
     return self.cleaned_data 

    def save(self, commit=True): 
     user = super(MyRegistrationForm, self).save(commit=False) 
     user.set_password(self.cleaned_data['password1']) 
     user.set_adult # NOT WORKING 
     if commit: 
      user.save() 
     return user 

"성인 생일"을 추가 한 후 내 관리자 페이지를 확인하면 결과가 항상 거짓입니다.

내 양식이나 내 set_adult 방법으로 실수를 저지른가요?

답변

2
user.set_adult # NOT WORKING 

당신은 함수 호출에 () 누락 :

user.set_adult() 
+0

와우. 바보 같지만 지금은 작동합니다. 정말 고맙습니다. 파이썬의 유연한 구문은 때로는 고통입니다. – erip