如何打印Django日期/时间字段?

时间:2016-09-16 17:52:46

标签: python django

我试图通过“str”函数在shell中打印Django日期/时间字段,但是我收到错误:

class Foo(models.Model):
    owner = models.ForeignKey(User, db_index=True, editable=False)
    dt_tm = models.DateTimeField(db_index=True)
    def __str__(self):
        return 'owner: ' + str(self.owner) + ', date / time: ' + 
            str(self.dt_tm)

>>> foo = Foo(user=mr_bar, dt_tm=now())
>>> foo
...
TypeError: Can't convert 'datetime.datetime' object to str implicitly

我对日期/时间格式并不挑剔,因为我不会在我的应用程序的其他地方使用它,只是在shell中。

0 个答案:

没有答案