1

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?

Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
Shikhar
  • 21
  • 3

3 Answers3

3

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
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
1

I assume you're talking about setting an auto-value on a database column.

This link has the answer to your question, almost exactly. Be sure to note what version of Django you're using.

Sam Bobel
  • 1,784
  • 1
  • 15
  • 26
0

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)
miketery
  • 111
  • 1
  • 7