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 }}
.
Comments
There are currently no comments
New Comment