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

(111 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 }}.

Current rating: 5

Comments

Comment deleted 2 months, 3 weeks ago

Comment deleted 2 months, 3 weeks ago

Comment deleted 2 months, 3 weeks ago

Comment deleted 2 months, 3 weeks ago

Comment deleted 2 months, 3 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months, 1 week ago

Comment deleted 2 months ago

Comment awaiting approval 1 month, 4 weeks ago

Comment awaiting approval 1 month, 4 weeks ago

Comment awaiting approval 1 month, 3 weeks ago

Comment awaiting approval 1 month, 3 weeks ago

Comment awaiting approval 1 month, 3 weeks ago

Comment awaiting approval 1 month, 3 weeks ago

Comment awaiting approval 1 month, 3 weeks ago

Comment awaiting approval 1 month, 3 weeks ago

Comment awaiting approval 1 month, 3 weeks ago

Comment awaiting approval 1 month, 3 weeks ago

Comment awaiting approval 1 month, 3 weeks ago

Comment awaiting approval 1 month, 2 weeks ago

Comment awaiting approval 1 month, 2 weeks ago

Comment awaiting approval 1 month, 2 weeks ago

Comment awaiting approval 1 month, 2 weeks ago

Comment awaiting approval 1 month, 2 weeks ago

Comment awaiting approval 1 month, 2 weeks ago

Comment awaiting approval 1 month, 2 weeks ago

Comment awaiting approval 1 month, 1 week ago

Comment awaiting approval 3 weeks, 3 days ago

Comment awaiting approval 3 weeks, 1 day ago

Comment awaiting approval 1 day, 19 hours ago

New Comment

required

required (not published)

optional

required

Recent Posts

Archive

2018
2014
2012
2011
2010
2009
2008
2007

Categories

Authors

Feeds

RSS / Atom