简体   繁体   English

自定义user_logged_in信号之前django更新last_login

[英]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.

相关问题 Django rest auth user_logged_in 信号 - Django rest auth user_logged_in signal 在Django中使用all-auth接收到user_logged_in信号后,将用户设置为AnonymousUser - User is set to AnonymousUser after a user_logged_in signal is received in Django with all-auth 如何更改自定义Django用户创建的数据库列last_login的名称? - How to change the name of db column last_login created by custom Django user? 获取特定用户的last_login时间? (django) - Get last_login time for a certain user? (django) 运行测试用例时,Django的user_logged_in信号不起作用 - Django's user_logged_in signal not working when running a test case 无法更新Django内置用户的last_login字段 - Unable to Update last_login field of Django in-built user 在 Django 中显示加入日期和 last_login - Display join date and last_login in Django 每次我尝试将用户添加到数据库时,Django中的date_joined和last_login都会出现错误 - date_joined and last_login required error in Django every time i tried to add a User to the database 从Django 1.6(南方)升级到1.8不会修改用户表上的“last_login” - Upgrading from Django 1.6 (with south) to 1.8 doesn't modify 'last_login' on the user table django-registration(1048,“Column'last_login'不能为null”) - django-registration (1048, “Column 'last_login' cannot be null”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM