[英]Custom user_logged_in signal BEFORE django updates last_login
I'd like to show a notification to user with some stats (eg how many items have been sold since last time he logged in) 我想向用户显示一些统计信息(例如,自上次登录后已售出多少件物品)
@receiver(user_logged_in)
def notify_user_on_login(sender, request, user, **kwargs):
items = Item.objects.filter(status=Item.HISTORY_STATUS, seller=user, when_trading__gte=user.last_login)
However, in this signal last_login
has already been updated. 但是,在此信号中,
last_login
已经更新。 According to the source at django.contib.auth
django also connects signal with function that updates last_login
: 根据
django.contib.auth
的消息来源,django还将信号与更新last_login
函数连接:
user_logged_in.connect(update_last_login)
Is it possible to call my function BEFORE updating? 是否可以在更新之前调用我的功能? Or get same result without adding custom field or doing some strange magic?
或者在不添加自定义字段或做一些奇怪的魔法的情
The last_login
is also updated with a handler to that signal, which is surely registered and executed before yours. last_login
也会使用该信号的处理程序进行更新,该处理程序肯定会在您的信号之前注册并执行。 You might be able to solve your issue by moving your app over django.contrib.auth
in INSTALLED_APPS
. 您可以通过将应用程序移动到
INSTALLED_APPS
django.contrib.auth
来解决您的问题。
Signal handlers depending on order doesn't seem like a good idea though. 信号处理程序依赖于顺序似乎不是一个好主意。 So I would probably replace Django's handler with your own:
所以我可能用你自己的代替Django的处理程序:
from django.contrib.auth.models import update_last_login
def notify_user_on_login(user):
items = Item.objects.filter(status=Item.HISTORY_STATUS, seller=user, when_trading__gte=user.last_login)
@receiver(user_logged_in)
def after_user_logged_in(sender, user, **kwargs):
notify_user_on_login(user)
update_last_login(sender, user, **kwargs)
user_logged_in.disconnect(update_last_login)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.