[Fixed]-Automatically generating last_login and date_joined values for AuthUser


DateTimeFields have an auto_now_add parameter

date_joined = models.DateTimeField(auto_now_add=True)

Automatically set the field to now when the object is first created. Useful for creation of timestamps. Note that the current date is always used; it’s not just a default value that you can override. So even if you set a value for this field when creating the object, it will be ignored. If you want to be able to modify this field, set default=timezone.now (from django.utils.timezone.now()) instead of auto_now_add=True.

Last login is a little bit harder since you need to determine when a user has actually logged in, this one will require manual intervention. Django appears to do this in its login view by sending the update_last_login signal

def update_last_login(sender, user, **kwargs):
    A signal receiver which updates the last_login date for
    the user logging in.
    user.last_login = timezone.now()

Leave a comment