2014-11-27 3 views
2

계정 관리를 위해 웹 응용 프로그램에 django-allauth을 사용하고 있습니다.djanago-allauth로 post_save 신호를 보낸 후 확인 메일을 보내는 방법

사용자 모델과 사용자 프로필 모델이 있습니다.

사용자가 가입하면 사용자 (사용자 모델의 인스턴스)가 생성됩니다.

UserProfile은 User 모델과 연결됩니다.

Allauth는 기본적으로 사용자가 Up으로 서명 할 때 전자 메일을 보냅니다. 가입 할 때 확인 전자 메일이 사용자에게 전송됩니다.

이제 UserProfile을 채울 때만 그 이메일을 보내려고합니다.

여기 신호를 사용하려고합니다. 사용자가 UserProfile을 채울 때 다른 함수를 호출하는 post_save 신호를 호출합니다. user_signed_up_ 그러나 요청 및 사용자 args를 해당 함수에 전달할 때 문제가 있습니다.

여기 당신은 여기 allauth의 신호를 사용 할 필요가없는 내 models.py

from django.db import models 
from django.contrib.auth.models import User 
from allauth.account.signals import user_signed_up 
from allauth.account.utils import send_email_confirmation 
from django.dispatch import receiver 
import datetime 

class UserProfile(models.Model): 
    user = models.ForeignKey(User, unique=True) 

    are_u_intrested = models.BooleanField(default=False) 


#this signal will be called when user signs up.allauth will send this signal. 

@receiver(user_signed_up, dispatch_uid="some.unique.string.id.for.allauth.user_signed_up") 
def user_signed_up_(request, user, **kwargs): 

    send_email_confirmation(request, user, signup=True) 
    #this allauth util method, which sends confirmation email to user. 





models.signals.post_save.connect(user_signed_up_, sender=UserProfile, dispatch_uid="update_stock_count") 

#If user fills this userProfile , then I only send confirmation mail. 
#If UserProfile model instace saved , then it send post_save signal. 
+0

'django-allauth'에 익숙하지 않지만'post_save'가 있다면 - 왜 사용자 인수를해야합니까? - 당신은'instance.user'를 사용하여 그것을 얻을 수 있습니다. https://docs.djangoproject.com/en/dev/ref/signals/#post-save – madzohan

+0

사실 요청 객체도 필요합니다. – user3513758

+0

그러면 다른 방법을 찾아야합니다. 그렇지 않으면 신호 처리기 앞에'instance'를 변경하고 필요한 _ _attrs를 추가해야합니다. http://stackoverflow.com/a/11388127/3033586 – madzohan

답변

2

, 당신은 장고의 모델 신호를 사용해야합니다. 다음과 같이 입력하십시오 :

from django.db.models.signals import post_save 
from django.dispatch import receiver 

@receiver(post_save, sender=UserProfile) 
def userprofile_filled_out(sender, instance, created, raw, **kwargs): 
    # Don't want to send it on creation or raw most likely 
    if created or raw: 
     return 

    # Test for existence of fields we want to be populated before 
    # sending the email 
    if instance.field_we_want_populated: 
     send_mail(...) 

희망 하시겠습니까?