user3199840
user3199840

Reputation: 535

Django 1.8 Durationfield and time difference

class Result(models.Model):
    starttime = models.TimeField(blank=True)
    endtime = models.TimeField(blank=True)
    duration = models.DurationField(blank=True)

    def save(self, *args, **kwargs):
        self.duration = self.endtime - self.starttime 
        super(Result, self).save(*args, **kwargs)

What's the correct way of doing this? I get:

Upvotes: 1

Views: 450

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599480

You can't subtract standalone times from each other. For example, what would "12pm - 3pm" be?

You can subtract datetimes, since they represent actual points in time. You should probably use DateTimeField instead.

Upvotes: 2

Related Questions