Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get session from signal handler in Django

Tags:

django

I'm implementing a simple referral system. I have middleware which sets a session variable identifying the referring user. I have a model which ties a referring user to the referred user. I'm trying to use the post_save signal from the User object to populate this model. How do I access the session object from within the post_save signal handler?

like image 825
Micah Carrick Avatar asked Aug 31 '11 22:08

Micah Carrick


People also ask

How do you call a signal in Django?

Sending signals There are two ways to send signals in Django. To send a signal, call either Signal. send() (all built-in signals use this) or Signal. send_robust() .

Where is Pre_save signal in Django?

pre_save. This is sent at the beginning of a model's save() method. Arguments sent with this signal: sender.

What is Post_save in Django?

Tips to use post_save As the name suggests, it is called just after the Django model save() function has done its job. The sender parameter here defines the Model from which we are trying to receive the signal.

What is session in Django?

Sessions are the mechanism used by Django (and most of the Internet) for keeping track of the "state" between the site and a particular browser. Sessions allow you to store arbitrary data per browser, and have this data available to the site whenever the browser connects.


1 Answers

There is not way without using a thread specific global variable.

But I'm not sure you need to. For my referral and invite system I just have the user register as normal and after the user has been created, get the referral out of the session. In almost all situations it will still be the same session.

If there is something about your session that prevents that, I would instead add it to the create user form.

like image 123
Rob Osborne Avatar answered Oct 01 '22 01:10

Rob Osborne