如何从Django中的信号处理程序获取会话

时间:2021-07-09 20:50:18

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?

我正在实施一个简单的推荐系统。我有中间件,它设置一个标识引用用户的会话变量。我有一个模型,它将推荐用户与推荐用户联系起来。我正在尝试使用User对象的post_save信号来填充此模型。如何从post_save信号处理程序中访问会话对象?

2 个解决方案

#1


1  

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.

如果您的会话中有某些内容阻止了这一点,我会将其添加到创建用户表单中。

#2


0  

You may find useful documentation on using sessions out of views.

您可以找到有关在视图外使用会话的有用文档。

#1


1  

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.

如果您的会话中有某些内容阻止了这一点,我会将其添加到创建用户表单中。

#2


0  

You may find useful documentation on using sessions out of views.

您可以找到有关在视图外使用会话的有用文档。