さて、再びこれを試してみましょう..
catsused = {}
posts = {}
postindex = 0
posts.add(Post.objects.order_by('date')[postindex])
catsused.add(Post.objects.order_by('date')[postindex].category
for each in range(1,5):
postindex = postindex + 1
post = Post.objects.order_by('date')[postindex]
while post.category in catsused:
postindex = postindex + 1
post = Post.objects.order_by('date')[postindex]
posts.add(post)
catsused.add(post.category)
posts ## Now contains your posts
それは私にとって恐ろしいコードのようですが、それに近いものが仕事をすると思います。あなたは、システム内にある投稿の数を上回る「postindex」を扱うためのコードをいくつか追加したいと思うでしょう。あなたはそれまでに注文が5最大限に活用することができ、
catsused = {}
posts = {}
for post in Post.objects.order_by('date'):
if post.category not in catsused:
posts.add(post)
catsused.add(post.category)
if len(catsused) == 5:
break
最近更新された5つのカテゴリをクエリし、後でこれらのカテゴリのそれぞれからトップポストを取得できませんでしたか? –
カテゴリにフィールド "更新時間"がない...つまり、どういうわけか質問で検索できますか? – Friendka