Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django admin users timein and timeout of the user

Hi I am new in Django framework, I just want to know if is it possible to capture the timein and timeout of the user, I have this model that if the employee1 login on django-admin it will add automatic record and if the user click logout it will update the time-out. What I want is for every time the employee login in django admin it will add record and it will update the record once the employee logout, just like daily attendance record on company.

class UserLogs(models.Model):
    user = models.OneToOneField(User, related_name="profile", on_delete=models.CASCADE)
    time_in = models.DateTimeField(default=now)
    time_out = models.DateTimeField(null=True, blank=True)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

you can use the Login and logout signals: user_logged_in, user_logged_out and user_login_failed signals to do this. Documentation

Here is an example usage (add these to your models):

@receiver(user_logged_in)
def post_login(sender, request, user, **kwargs):
    print(f'User: {user.username} logged in')
    user.time_in = datetime.now()
    user.save()

This acts as a "Watcher" function that triggers when the user logs in. Similar for logging out.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading