Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将datetime.utcnow()与DjangoTables2 render\u FOO一起使用_Python_Django_Python 2.7_Django 1.4_Django Tables2 - Fatal编程技术网

Python 将datetime.utcnow()与DjangoTables2 render\u FOO一起使用

Python 将datetime.utcnow()与DjangoTables2 render\u FOO一起使用,python,django,python-2.7,django-1.4,django-tables2,Python,Django,Python 2.7,Django 1.4,Django Tables2,我试图用DjangoTables 2显示帖子的年龄(以小时为单位)。我的代码如下 class PostTable(tables.Table): current_Time = datetime.utcnow().replace(tzinfo=utc) published= tables.Column() def render_published(self, value,record): tdelta = self.current_Time - record.

我试图用DjangoTables 2显示帖子的年龄(以小时为单位)。我的代码如下

class PostTable(tables.Table):
    current_Time = datetime.utcnow().replace(tzinfo=utc)
    published= tables.Column()
    def render_published(self, value,record):
        tdelta = self.current_Time - record.published
        #Some logic 
使用此代码,“当前时间”仅在apache服务器重新启动时更新。如果我将代码更改为

  tdelta = datetime.utcnow().replace(tzinfo=utc) - record.published

它可以工作,但会为每一行计算datetime.utcnow(),这是无效的。我希望表的“当前时间”只更新一次。实现这一点的最佳方法是什么?

当前时间
是在读入类定义时在类上安装的字段。这在最初定义类时发生一次。在您的情况下,这发生在服务器启动时。
current_Time
的值在那时设置,并且仅设置一次

您需要将
current\u Time=datetime.utcnow().replace(tzinfo=utc)
移动到
def render\u published(self、value、record)中:


这样,每次调用render_published方法时,都会填充当前时间。

请尝试在表的
\uuuuuu init\uuu
方法中设置当前时间。然后,
self.current_Time
将在每次启动表格时设置,而不是在定义表格时设置

class PostTable(tables.Table):
    def __init__(self, *args, **kwargs):
        super(PostTable, self).__init__(*args, **kwargs)
        self.current_Time =  datetime.utcnow().replace(tzinfo=utc)

    def render_published(self, value,record):
        tdelta = self.current_Time - record.published

虽然不是答案,
datetime.utcnow().replace(tzinfo=utc)
可以写成
datetime.now(pytz.utc)
。谢谢,但它与tdelta=datetime.utcnow()相同。replace(tzinfo=utc)-record.published,它将为每一行调用,不是吗?每次调用render\u published()时都会调用它@阿拉斯代尔提供了一个只调用一次的答案。但是,如果在对象实例化和render_发布之间经过了很长时间,tdelta将在其实现中关闭。想象一下,运行一个长报告,首先加载PostTable,经过几秒钟的处理后,调用render_published()。这取决于您需要tdelta的精确程度以及您计划如何使用它。谢谢。它起作用了。self.current_Time=datetime.utcnow().replace(tzinfo=utc)足够好了,不是吗?
class PostTable(tables.Table):
    def __init__(self, *args, **kwargs):
        super(PostTable, self).__init__(*args, **kwargs)
        self.current_Time =  datetime.utcnow().replace(tzinfo=utc)

    def render_published(self, value,record):
        tdelta = self.current_Time - record.published