I am creating a "library" website using django.
Once a user has issued a book and I want to calculate a due date for it which will 3 months after the issue/present date.
How do I do it?
I am creating a "library" website using django.
Once a user has issued a book and I want to calculate a due date for it which will 3 months after the issue/present date.
How do I do it?
Use the datetime
module and you can add a timedelta of 90 days very easily:
>>> import datetime
>>> print datetime.datetime.now()
2017-10-23 19:24:19.549670
>>> print datetime.datetime.now() + datetime.timedelta(days=90)
2018-01-21 19:24:21.583689
You'll want to override the save
method for the model which tracks the book transaction.
from datetime import datetime, timedelta
class BookTransaction(models.Model):
...
def save(self, *args, **kwargs):
# if first time creating transaction (as opposed to on update)
if not self.id:
self.due_date = datetime.now() + timedelta(months=3)
# call and return default save method after custom rule
return super(Base, self).save(*args, **kwargs)