1

我正在尝试使用 Django 的站点地图框架功能。我已经实现了代码,它适用于当前文章对象 post_date。但是,我正在尝试使用以下代码获得更准确的最后修改日期,但它给了我错误。错误追溯 http://dpaste.com/3Z04VH8

问候。谢谢你的帮助

from django.contrib.sitemaps import Sitemap
from django.db.models import Max
from article.models import Article


class ArticleSitemap(Sitemap):
    changefreq = 'hourly'
    priority = 0.5

    def items(self):
        return Article.objects.all()

    def lastmod(self, obj):
        from post.models import Post
        return Post.objects.filter(article=obj).aggregate(Max('post_date'))
        #return obj.post_date
4

1 回答 1

1

类中的lastmod方法Sitemap需要返回一个datetime对象。相反,您正在返回一个字典(这aggregate将产生) - 这是无效的。

您需要从该字典中获取数据并返回:

result = Post.objects.filter(article=obj).aggregate(Max('post_date'))
# result will look something like {'post_date__max': Datetime('2017-12-06')}
return result['post_date__max']
于 2017-12-07T02:53:07.667 回答