I tried subtracting two times in models.py but I got error. Here is my model That I have been working.
JavaScript
x
4
1
class Schedule(BaseModel):
2
bus_company_route = models.ForeignKey(BusCompanyRoute, on_delete=models.PROTECT)
3
travel_date_time = models.DateTimeField()
4
and BusCompanyRoute
have journey length.
JavaScript
1
3
1
class BusCompanyRoute(BaseModel):
2
journey_length = models.TimeField(null=True)
3
Now I tried adding these times in using @property
decorator in following way
JavaScript
1
4
1
@property
2
def journey_end_time(self):
3
return self.travel_date_time.time()+self.bus_company_route.journey_length
4
but end up getting following error warning as:
Class 'time' does not define '__add__', so the '+' operator cannot be used on its instances
How can I solve it?
Advertisement
Answer
A DurationField
is more appropriate for storing the duration of a journey not a TimeField
, this will give you a timedelta that you can add or subtract from a datetime whereas a TimeField
just gives a static time of day
JavaScript
1
3
1
class BusCompanyRoute(BaseModel):
2
journey_length = models.DurationField(null=True)
3