2016-01-07 26 views
0

我正在用户模型中注册数据,并且还希望像概要文件模型中的first_name和last_name那样同时保存配置文件数据。我们是否可以在信号中获取请求对象数据

所以,我用django信号来存储配置文件信息并将邮件发送给用户。

但我们无法获得信号文件中的FIRST_NAME和LAST_NAME:

 #---------------------------- Create profile at the time of registration --------------------------# 
    def register_profile(sender, **kwargs): 
     if kwargs.get('created'): 
      user = kwargs.get('instance') 
      request = kwargs.get("request") 
      if user.id is not None and user._disable_signals is not True: 
        profile = Profile(user=user) 
        if user.status is not 1: 
         #------------------- Send the registration mail to user and it have confirmation link ----------# 
         salt = hashlib.sha1(str(random.random())).hexdigest()[:5]    
         activation_key = hashlib.sha1(salt+user.email).hexdigest()    
         key_expires = datetime.datetime.today() + datetime.timedelta(2)      
         #print user 
         profile.activation_key = activation_key 
         profile.key_expires = key_expires 
        #--------------------- End -------------------------------------------------------------# 

        profile.save() 
        if user.status is not 1: 
         user = model_to_dict(user) 
         BaseSendMail.delay(user,type='account_confirmation',key = activation_key) 
        return 
    post_save.connect(register_profile, sender=User, dispatch_uid='register_profile') 
    #-------------------------- End ---------------------------------------------------------------------# 

在上面的代码中,我无法得到FIRST_NAME并在registration.Also的发送时间,我想姓氏数据提及first_name和last_name字段属于配置文件模型。

回答

2

不,你不应该尝试。信号可以从任何地方执行:管理脚本,Celery任务,可能没有请求的各种地方。

您可以暂时将数据存储在用户实例上,就像您使用_disable_signals属性一样。然而我怀疑这并不是最好的信号;既然你保存了表单提交的结果,并且依赖于表单中的数据,你应该在视图或表单本身中这样做。

-1

我这样做,它的工作。 不知道它是对性能的影响等

some_file.py:

data = {} 

middleware.py:

class MyMiddleware(object): 

    def process_request(self): 

     from path.to.some_file import data 
     data['request'] = self.request 

信号/ model_method /经理/模板标签/任何地方比:

from path.to.some_file import data 
request = data.get('request') 
+0

你不应该在django中使用全局变量,并且导入不会实现你想要的东西ither。 –

相关问题