django DurationField - (very) simple way to format (for template)

(93 comments)

So I have a Django DurationField in my model, and needed to format this as HH:mm .. unfortunately django doesn't seem to support that out of the box.. after considering templatetags or writing my own filter I decided to go for a very simple alternative and just defined a method for this in my model:

    timeslot_duration = models.DurationField(null=False,
                                             blank=False,
                                             default='00:05:00',
                                             verbose_name=_('timeslot_duration'),
                                             help_text=_('[DD] [HH:[MM:]]ss[.uuuuuu] format')
                                             )

    def timeslot_duration_HHmm(self):
        sec = self.timeslot_duration.total_seconds()
        return '%02d:%02d' % (int((sec/3600)%3600), int((sec/60)%60))

that way I can do whatever I want format-wise to get exactly what I need. Not sure if this is recommended practice, or maybe frowned upon, but it works just fine.

and in my template then just use {{ <model>.timeslot_duration_HHmm }} instead of {{ <model>.timeslot_duration }}.

Currently unrated

Comments

Comment awaiting approval 3 weeks, 2 days ago

Comment awaiting approval 3 weeks, 2 days ago

Comment awaiting approval 3 weeks, 2 days ago

Comment awaiting approval 3 weeks, 1 day ago

Comment awaiting approval 2 weeks, 5 days ago

Comment awaiting approval 2 weeks, 1 day ago

Comment awaiting approval 2 weeks, 1 day ago

Comment awaiting approval 2 weeks ago

Comment awaiting approval 1 week, 6 days ago

Comment awaiting approval 1 week, 6 days ago

Comment awaiting approval 1 week, 6 days ago

Comment awaiting approval 1 week, 6 days ago

Comment awaiting approval 1 week, 6 days ago

Comment awaiting approval 1 week, 6 days ago

Comment awaiting approval 1 week, 6 days ago

Comment awaiting approval 1 week, 6 days ago

Comment awaiting approval 1 week, 5 days ago

Comment awaiting approval 1 week, 5 days ago

Comment awaiting approval 1 week, 5 days ago

Comment awaiting approval 1 week, 5 days ago

Comment awaiting approval 1 week, 5 days ago

Comment awaiting approval 1 week, 5 days ago

Comment awaiting approval 1 week, 5 days ago

Comment awaiting approval 1 week, 5 days ago

Comment awaiting approval 1 week, 5 days ago

Comment awaiting approval 1 week, 4 days ago

Comment awaiting approval 1 week, 4 days ago

Comment awaiting approval 1 week, 4 days ago

Comment awaiting approval 1 week, 4 days ago

Comment awaiting approval 1 week, 4 days ago

Comment awaiting approval 1 week, 4 days ago

Comment awaiting approval 1 week, 4 days ago

Comment awaiting approval 1 week, 4 days ago

Comment awaiting approval 1 week, 4 days ago

Comment awaiting approval 1 week, 4 days ago

Comment awaiting approval 1 week, 4 days ago

Comment awaiting approval 1 week, 4 days ago

Comment awaiting approval 1 week, 3 days ago

Comment awaiting approval 1 week, 3 days ago

Comment awaiting approval 1 week, 3 days ago

Comment awaiting approval 1 week, 3 days ago

Comment awaiting approval 1 week, 3 days ago

Comment awaiting approval 1 week, 3 days ago

Comment awaiting approval 1 week, 3 days ago

Comment awaiting approval 1 week, 2 days ago

Comment awaiting approval 1 week, 2 days ago

Comment awaiting approval 1 week, 2 days ago

Comment awaiting approval 1 week, 2 days ago

Comment awaiting approval 1 week, 2 days ago

Comment awaiting approval 1 week, 2 days ago

Comment awaiting approval 1 week, 2 days ago

Comment awaiting approval 1 week, 2 days ago

Comment awaiting approval 1 week, 2 days ago

Comment awaiting approval 1 week, 2 days ago

Comment awaiting approval 1 week, 2 days ago

Comment awaiting approval 1 week, 1 day ago

Comment awaiting approval 1 week, 1 day ago

Comment awaiting approval 1 week, 1 day ago

Comment awaiting approval 1 week, 1 day ago

Comment awaiting approval 1 week, 1 day ago

Comment awaiting approval 1 week, 1 day ago

Comment awaiting approval 1 week, 1 day ago

Comment awaiting approval 1 week, 1 day ago

Comment awaiting approval 1 week, 1 day ago

Comment awaiting approval 1 week, 1 day ago

Comment awaiting approval 1 week ago

Comment awaiting approval 1 week ago

Comment awaiting approval 1 week ago

Comment awaiting approval 1 week ago

Comment awaiting approval 1 week ago

Comment awaiting approval 1 week ago

Comment awaiting approval 1 week ago

Comment awaiting approval 1 week ago

Comment awaiting approval 1 week ago

Comment awaiting approval 1 week ago

Comment awaiting approval 1 week ago

Comment awaiting approval 1 week ago

Comment awaiting approval 1 week ago

Comment awaiting approval 1 week ago

Comment awaiting approval 1 week ago

Comment awaiting approval 6 days, 23 hours ago

Comment awaiting approval 6 days, 15 hours ago

Comment awaiting approval 6 days, 12 hours ago

Comment awaiting approval 6 days, 9 hours ago

Comment awaiting approval 6 days, 9 hours ago

Comment awaiting approval 5 days, 19 hours ago

Comment awaiting approval 5 days, 14 hours ago

Comment awaiting approval 4 days, 11 hours ago

Comment awaiting approval 4 days, 10 hours ago

Comment awaiting approval 2 days, 22 hours ago

Comment awaiting approval 2 days, 14 hours ago

Comment awaiting approval 2 days, 11 hours ago

Comment awaiting approval 11 hours, 49 minutes ago

New Comment

required

required (not published)

optional

required