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

(117 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 4 months, 2 weeks ago

Comment deleted 4 months, 2 weeks ago

Comment deleted 4 months, 2 weeks ago

Comment deleted 4 months, 2 weeks ago

Comment deleted 4 months, 2 weeks ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months, 1 week ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 4 months ago

Comment deleted 3 months, 3 weeks ago

Comment deleted 3 months, 3 weeks ago

Comment deleted 3 months, 2 weeks ago

Comment deleted 3 months, 2 weeks ago

Comment awaiting approval 3 months, 2 weeks ago

Comment deleted 3 months, 2 weeks ago

Comment deleted 3 months, 2 weeks ago

Comment deleted 3 months, 2 weeks ago

Comment deleted 3 months, 2 weeks ago

Comment deleted 3 months, 2 weeks ago

Comment deleted 3 months, 2 weeks ago

Comment deleted 3 months, 2 weeks ago

Comment deleted 3 months, 1 week ago

Comment deleted 3 months, 1 week ago

Comment deleted 3 months, 1 week ago

Comment deleted 3 months, 1 week ago

Comment deleted 3 months, 1 week ago

Comment deleted 3 months, 1 week ago

Comment deleted 3 months ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 2 months, 2 weeks ago

Comment deleted 1 month, 3 weeks ago

Comment deleted 1 month, 2 weeks ago

Comment deleted 1 month, 2 weeks ago

Comment deleted 1 month, 2 weeks ago

Comment deleted 1 month ago

Comment awaiting approval 1 week, 5 days ago

Comment awaiting approval 1 week, 2 days ago

New Comment

required

required (not published)

optional

required