仅按最新对象排序

时间:2019-02-22 01:59:22

标签: django django-models

Models.py

    class Post(models.Model):
        article_title = models.CharField(max_length=100)
        content = models.TextField()
        date_published = models.DateTimeField(db_index=True, default=timezone.now)
        game = models.ForeignKey('library.Game', on_delete=models.CASCADE)
        article_image = models.ImageField(default='/media/default.png', upload_to='article_pics')
        platform = models.CharField(max_length=20)

    class Game(models.Model):
        title = models.CharField(max_length=100)
        description = models.TextField()
        date_posted = models.DateTimeField(default=timezone.now)
        cover = models.ImageField()
        cover_display = models.ImageField(default='default.png')
        developer = models.CharField(max_length=100)
        twitter = models.CharField(max_length=50, default='')

Views.py

'recent': Game.objects.all().order_by('-post__date_published')[:5],

如何检索每个游戏的最新帖子,然后按发布的最新帖子的日期对游戏进行排序?我目前在views.py中使用whats,但是它将对所有帖子进行排序,如果某个游戏最近有很多帖子,它将返回重复项。

整个views.py

from django.shortcuts import render, get_object_or_404
from library.models import Game
from .models import Post
from django.views.generic import (
    ListView,
    DetailView
)

# Create your views here.
def home(request):

    context = {
        'recent': Game.objects.all().order_by('-post__date_published')[:5],
        'recent_posts1': Post.objects.all().order_by('-date_published')[:1],
        'recent_posts2': Post.objects.all().order_by('-date_published')[1:2],
        'recent_posts3': Post.objects.all().order_by('-date_published')[2:3],
    }
    return render(request, 'main/home.html', context)

class TitlePostListView(ListView):
    model = Post
    template_name = 'main/title_posts.html'
    context_object_name = 'posts'
    paginate_by = 5


    def get_queryset(self):
        title = get_object_or_404(Game, title=self.kwargs.get('title'))
        return Post.objects.filter(game=title).order_by('-date_published')

    def get_context_data(self, **kwargs):
        context = super(TitlePostListView, self).get_context_data(**kwargs)
        context['game'] = get_object_or_404(Game, title=self.kwargs.get('title'))
        return context

class PostDetailView(DetailView):
    model = Post

2 个答案:

答案 0 :(得分:0)

更新的答案,只需指定您需要唯一的元素

'recent': Game.objects.all().order_by('-post__date_published').distinct()[:5],

答案 1 :(得分:0)

我不确定您为什么发送了多个最近的帖子,可以在单个上下文中发送。当您有多个相同游戏的帖子时,现在解决您的问题。如果使用的是Postgresql,则可以使用distinct轻松解决。例如这样的

context = {
    'recent': Game.objects.all().order_by('-post__date_published')[:5],
    'recent_posts': Post.objects.all().order_by('-date_published').distinct('game')[:5]
}

在模板中像这样使用它:

{% for post in recent_posts %}
    Last {{ forloop.counter }} Post: {{ post }}
{% endfor %}

但是,如果您不使用postgresql,它将有些复杂。但是,可以这样做:

posts = Post.objects.all().order_by('-date_published')
recent_post = []
for post in posts:
   if not filter(lambda x:x.game == post.game, recent_post):
      recent_post.append(post)
   if len(recent_post) == 5:
      break

context = {
    'recent': Game.objects.all().order_by('-post__date_published')[:5],
    'recent_posts': recent_post
}