user3048148
user3048148

Reputation: 195

Can we get the request object data in signal

I am registering the data in user model and also want to save the profile data same time like first_name and last_name in profile model.

So I have used django signals to store the profile information and send the mail to user.

But we are unable to get the first_name and last_name in signal file:

        #---------------------------- 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 ---------------------------------------------------------------------#

In above code I am unable to get first_name and last_name data which is sent at the time of registration.Also I would like to mention that first_name and last_name fields belong to profile model.

Upvotes: 0

Views: 3126

Answers (2)

Vinayak Kaniyarakkal
Vinayak Kaniyarakkal

Reputation: 1120

I did this and it worked. Not sure about it's impact on performance etc.

some_file.py:

data = {}

middleware.py:

class MyMiddleware(object):

    def process_request(self):

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

signals / model_method / manager / template tag / any where else:

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

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599778

No, and you shouldn't try. Signals could be executed from anywhere: a management script, a Celery task, all sorts of places that might not have a request.

You could store the data on the User instance temporarily, as you do with the _disable_signals attribute. However I suspect that this is not really best done in a signal; since you're saving the result of a form submission, and it depends on the data in that form, you should really do that in the view or the form itself.

Upvotes: 4

Related Questions