How du you calculate the time since a model was created? The model has the following field:
created_at = models.DateTimeField(auto_now_add=True)
This is what I tried:
from datetime import datetime, timedelta
@property
def time_since_created(self):
return (datetime.now()-self.created_at).total_seconds()
What I do not understand is that it crashes without giving me any error messages. If I wrap it in a try/except block like this:
@property
def time_since_created(self):
try:
return (datetime.now()-self.created_at).total_seconds()
except Exception as e:
try:
print("Error:" + e)
except:
print("Error: An exception occured when trying to print exception")
The error I will get is "Error: An exception occured when trying to print exception"
. If I do not include the last try/catch block I won't get any output at all.
Do anyone have any ideas?