Search code examples
djangoformattypeerrortimefield

How to print a TimeField - TypeError: not all arguments converted during string formatting


This is my model:

class Meeting(models.Model):
start_time = models.TimeField(null=True)

def __unicode__(self):
    return u'%s' % (str(self.start_time))

When I do Meeting.objects.all()

I get

TypeError: not all arguments converted during string formatting

Hopefully this is an easy question! Thanks a lot :)


Solution

  • The TimeField stores time in a python datetime instance. From the django docs:

    class TimeField(auto_now=False, auto_now_add=False, **options)

    A time, represented in Python by a datetime.time instance.

    You should be able to print the datetime object directly to the console, but if you need to convert it to a string, you should always use python's built in time framework. Specifically, strftime()

    https://docs.python.org/2/library/time.html#time.strftime