计数特定用户登录的数量django? [英] count number of logins by specific user django?
本文介绍了计数特定用户登录的数量django?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
auth_user的last_login字段将随每次登录更新。
我们可以使用该字段来计算特定用户的登录数量吗?
解决方案
是的,在感。您需要使用应用程序的 UserProfile
模型中的字段来保存登录号或用于存储完整登录历史记录的单独模型。然后为last_login更新添加信号处理程序,并将其记录在您选择的模型中。这是我的示例:
从django.db导入模型,信号来自django.contrib.auth.models的用户
导入用户
class UserLogin(models.Model):
表示用户登录,每记录一个
user = models.ForeignKey(user)
timestamp = models.DateTimeField()
def user_presave(sender,instance,** kwargs):
if instance.last_login:
old = instance .__ class __。objects.get( pk = instance.pk)
if instance.last_login!= old.last_login:
instance.userlogin_set.create(timestamp = instance.last_login)
signals.pre_save.connect( user_presave,sender = User)
Is their any way of counting number of django logins? The last_login field of the auth_user gets updated with each login. Can we make use of that field to count number of logins by a specific user?
解决方案
Yes, in a sense. You'll need either a field in you app's UserProfile
model to hold number of logins or a separate model for storing full login history. Then add signal handlers for last_login updates and record them in a model of your choice. Here's my example:
from django.db import models, signals
from django.contrib.auth.models import User
class UserLogin(models.Model):
"""Represent users' logins, one per record"""
user = models.ForeignKey(user)
timestamp = models.DateTimeField()
def user_presave(sender, instance, **kwargs):
if instance.last_login:
old = instance.__class__.objects.get(pk=instance.pk)
if instance.last_login != old.last_login:
instance.userlogin_set.create(timestamp=instance.last_login)
signals.pre_save.connect(user_presave, sender=User)
这篇关于计数特定用户登录的数量django?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文