python - 在 Lastmod 上使用聚合 max 函数时,Django 的站点地图框架给出错误

标签 python django sitemap django-2.0

我正在尝试使用 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

最佳答案

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']

关于python - 在 Lastmod 上使用聚合 max 函数时,Django 的站点地图框架给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47686487/

相关文章:

Python 正则表达式前瞻

seo - 站点地图中的主页

sql - Django:生成博客的事件条目列表。这有效率吗?

model-view-controller - MVCSiteMapProvider 不显示 SiteMapPath

python - 如何为 Mayavi 的网格图使用掩码?

Python 新手问题 - 未打印正确的值

python - 如何在列表中换行?

python - Django 中的负整数

python - 在更新 Redux 状态之前规范化来自服务器的 JSON 响应