1
Your code has several defects. First, it’s not clear for us what unit does duration
have, days, months, years, minutes?
I assume you were using days, but you couldn’t query that because you need to convert start_time + duration
to datetime and treat it as end time for each record, which is not possible in single query. As @solarissmoke mentioned, you should store the end_time
instead. You could keep the duration
field if you want, but you could also use a property method to calculate it.
Also, your field definition start_time = models.DateTimeField(default=datetime.now())
would not work, because the default time would only be evaluated once when django starts(when the model is loaded). It’s a common mistake that new django developers make. You should change it to:
start_time = models.DateTimeField(default=datetime.now)
This case, datetime.now
would be treated as a function object and be called every time you create a new object, which is what you want. Note that django has the built in flag to do that: auto_now_add
.