Search code examples
djangodatetimefeed

Django feed framework: tzinfo error


I am using django feed framework. This is what I have in my feeds.py:

def item_pubdate(self, item):
    return item.posted

This is what I have in my Blog class in models.py:

posted = models.DateField(db_index=True, auto_now_add=True)

And I get this attribute error:

'datetime.date' object has no attribute 'tzinfo'

Solution

  • See https://docs.djangoproject.com/en/dev/ref/contrib/syndication/ for the requirements of def item_pubdate. This is because most feed formats technically require a full timestamp as the publish date.

    The function you define item_pubdate for a feed must return a python datetime.datetime object, not a datetime.date object. The difference being of course that the object can contain a specific time in addition to the date information.

    Therefore, you must use models.DateTimeField instead of models.DateField.

    --

    If you are stuck using the models.DateField, then you can have your feed class do the conversion:

    from datetime import datetime, time
    
    def item_pubdate(self, item):
        return datetime.combine(item.posted, time())
    

    And that should get your date converted to a datetime so that contrib.syndication accepts it.