I have the following Django model:
created=models.DateTimeField(auto_now_add=True)
I now need a model object method that shows the number of hours since it was created. I tried the following:
def hours_live(self):
diff=((datetime.datetime.now - self.created).seconds)/3600
return diff
but he chose TypeError
unsupported operand type(s) for -: 'builtin_function_or_method' and 'datetime.datetime'
Then i went with
def hours_live(self):
diff=((datetime.datetime.now() - self.created).seconds)/3600
return diff
My question is whether the expression datetime.datetime.now () is executed only once here or is it executed every time hour_live is called.
Will this work?
source
share