简体   繁体   中英

Accessing the time where a field has been updated (Django models)

I need to create a system where the user have to change his password every "x" number of days. I managed to achieve this by saving a UTC timestamp representing the time when the user has created/modified his password.

My question is, instead of saving the timestamp in the database, is it possible to get a timestamp representing the time when the field has been changed through some of django's built-in functions? This way I could access when was the last time the user has updated his password without having to add a dedicated field in the model to save the timestamp in it.

No, there is no field that shows you which field updated in which time, you should implement it your self

you should use set_password everywhere to manage update password time

password_updated_at = models.DateTimeField(_('password updated at'))

def set_password(self, raw_password):
    r = super(User, self).set_password(raw_password=raw_password)
    self.password_updated_at = timezone.now()
    self.save()
    return r

def is_password_updated_at_expired(self):
    duration = (timezone.now() - self.password_updated_at).total_seconds()
    return duration > settings.PASSWORD_UPDATE_EXPIRE_TIME, duration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM