Django feed框架:tzinfo错误

nix*_*win 2 django datetime feeds

我正在使用django feed框架.这就是我在feeds.py中的内容:

def item_pubdate(self, item):
    return item.posted
Run Code Online (Sandbox Code Playgroud)

这是我在models.py中的Blog类中的内容:

posted = models.DateField(db_index=True, auto_now_add=True)
Run Code Online (Sandbox Code Playgroud)

我得到这个属性错误:

'datetime.date' object has no attribute 'tzinfo'
Run Code Online (Sandbox Code Playgroud)

Raj*_*ani 16

有关要求,请参阅https://docs.djangoproject.com/en/dev/ref/contrib/syndication/def item_pubdate.这是因为大多数Feed格式在技术上需要一个完整的时间戳作为发布日期.

item_pubdate为feed 定义的函数必须返回python datetime.datetime对象,而不是datetime.date对象.当然,不同之处在于除了日期信息之外,对象还可以包含特定时间.

因此,您必须使用models.DateTimeField而不是models.DateField.

-

如果您遇到困难models.DateField,那么您可以让Feed类进行转换:

from datetime import datetime, time

def item_pubdate(self, item):
    return datetime.combine(item.posted, time())
Run Code Online (Sandbox Code Playgroud)

这应该将您的日期转换为日期时间,以便contrib.syndication接受它.