F
F
filmincer2016-05-16 23:25:19
Django
filmincer, 2016-05-16 23:25:19

How to get correct difference between date range from two models.DateField fields?

Hello.
I am building a book rental website. There are two model fields: start_date and end_date (the user sets the lease range). It was necessary to receive list from days when the book will be occupied.
I did it in the form of a model method (wrapped in @property):

class Book(models.Model):
........

  @property
  def get_date(self):
    return [str(self.end - datetime.timedelta(i)) for i in range((self.end - self.start).days)]

When testing this code, I see that it lists the entire range but always misses one
day . '2016-05-14', '2016-05-13'] (i.e. misses the first day) Any response would be greatly appreciated

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeniy _, 2016-05-16
@GeneD88

You get range(15-12) which will give you 3 results

return [str(self.end - datetime.timedelta(i)) for i in range((self.end - self.start).days+1)]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question