2012-07-19 45 views
4

views.py:如何成參數傳遞給一個信號

user = User.objects.create_user(username=username, email=email)   
user.password=password1 
#user.set_password(password1) 
user.save() 

models.py:

def create_user_profile(sender, instance, created, **kwargs): 
    if created: 
     #here i want user password unencrypted where I pass that parameter to this 
     signal 

     post_save.connect(create_user_profile, sender=User) 

回答

1

發送信號

有兩種方法來發送在Django發送信號。

Signal.send(sender, **kwargs) 

Signal.send_robust(sender, **kwargs) 

https://docs.djangoproject.com/en/dev/topics/signals/#sending-signals

在你的情況,你可以做這樣的事情

# not the real word code. 
Signal.send(User, raw_password="xxx") 

# raw password stays in **kwargs 

def create_user_profile(sender, instance, created, **kwargs): 

if created: 
     //here i want user password unencrypted where I pass that parameter to this 
     raw_pass = kwargs.get('raw_password') 
+0

注意,Django的發送者總是類,而不是實例。實際上,您可以將任何東西作爲發件人傳遞,但最好以與Django本身相同的方式執行此操作。 – simplylizz 2012-07-19 09:20:56

+0

@simplylizz是的,我解決它,謝謝。 – xiaowl 2012-07-19 09:31:24