2017-12-07 3 views
0

Djangoのサイトマップフレームワーク機能を使用しようとしています。私はコードを実装し、現在の記事オブジェクトpost_dateで動作します。しかし、私は次のコードを使用してより正確な最終更新日を取得しようとしていると、それは私にエラーを与える。エラーのトレースバックhttp://dpaste.com/3Z04VH8Djangoのsitemapフレームワークがlastmodでaggregate max関数を使用しているときにエラーを返しました

よろしくおねがいします。任意の助け おかげで

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 
+0

あなたは 'POST'モデルを示し、ここで問題になっているトレースバックを含める必要があります。 –

+1

リンクトレースバックは、ここで説明している問題に関連していないようです。 –

+0

@Blurpトレースバックの最後の行を確認しました ファイル "/home/ec2-user/www/kritikarw.gr/venv/lib64/python3.6/site-packages/django/contrib/sitemaps/__init__.py" (latest_lastmodはNoneまたはlastmod> latest_lastmod)):例外タイプ:/sitemap.xmlのTypeError例外: 'dict'と 'dict'のインスタンス間で '>'がサポートされていません どこかオブジェクトの比較が行われ、thatsなぜ例外がスローされる – chasank

答えて

0

Sitemapクラスのlastmod方法は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'] 
+0

ありがとう。私はそれがDatetimeのインスタンスを返すと思っていた。 – chasank

関連する問題