2017-12-23 5 views
0

私はチュートリアルに従っていますが、ユーザーのプロファイルフィールドにアクセスできることを除いて、すべてうまくいきました。ユーザーモデルで 'is_active'フラグを設定できました。 'user.profile.email_confirmed'は本当にまったく変わっていないので、私が間違っていることについてのアイディア。確認メールと、ユーザーのプロフィールフィールドにアクセスして変更する方法は?

models.py

class Profile(models.Model): 
    # relations 
    user = models.OneToOneField(User, on_delete=models.CASCADE) 
    email_confirmed = models.BooleanField(default=False) 

tokens.py

class AccountActivationTokenGenerator(PasswordResetTokenGenerator): 
    def _make_hash_value(self, user, timestamp): 
     return (
      six.text_type(user.pk) + six.text_type(timestamp) + 
      six.text_type(user.profile.email_confirmed) 
     ) 


account_activation_token = AccountActivationTokenGenerator() 

urls.py

url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z] 
            {1,13}-[0-9A-Za-z]{1,20})/$', 
           views.activate, name='activate'), 

views.py

def activate(request, uidb64, token): 
    try: 
     uid = force_text(urlsafe_base64_decode(uidb64)) 
     user = User.objects.get(pk=uid) 
    except (TypeError, ValueError, OverflowError, User.DoesNotExist): 
     user = None 

    if user is not None and account_activation_token.check_token(user, token): 
     user.is_active = True 
     user.profile.email_confirmed = True 
     user.save() 
     login(request, user) 
     return redirect('home') 
    else: 
     return render(request, 'account_activation_invalid.html') 

答えて

1

あなた

user.save() 

呼び出しの前に

user.profile.save() 

を挿入してください。

基本的なDBの観点から見ていると役立ちます。通常、リレーションは、別のデータベーステーブルのエントリへの外部キーを表す整数として格納されます。あなたはis_activeが今Trueに設定されていますが、プロファイルオブジェクトを変更しなかったとして、ユーザープロファイルは、以前のように、まだ同じ整数値であることをDBに書き込む

user.save() 

を実行したがって

ユーザーが持っている私が知る限り、Djangoはプロフィールオブジェクトも変更されていることを理解するために外に出ることはありません。

+0

あなたは正しいと思いますが、今度は "user.profile.email_confirmed"がTrueに設定されています。 – SUDAPP

関連する問題