Skip to content

Commit

Permalink
Merge pull request #79 from DjangoChinaOrg/fix-index-order
Browse files Browse the repository at this point in the history
'修复首页post排序问题'
  • Loading branch information
yangxg authored Jul 22, 2018
2 parents 1d2b098 + f66ecdb commit f39f23c
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions posts/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime

from django.db.models import Count, Max
from django.db.models.functions import Coalesce
from django.utils.timezone import now
from django_filters import rest_framework as filters
from rest_framework import permissions, serializers, status, viewsets
Expand All @@ -21,8 +22,8 @@
class PostViewSet(viewsets.ModelViewSet):
queryset = IndexPostListSerializer.setup_eager_loading(
Post.public.annotate(
latest_reply_time=Max('replies__submit_date')
).order_by('-pinned', '-latest_reply_time', '-created'),
latest_post_time=Coalesce(Max('replies__submit_date'), 'created')
).order_by('-pinned', '-latest_post_time'),
select_related=IndexPostListSerializer.SELECT_RELATED_FIELDS,
prefetch_related=IndexPostListSerializer.PREFETCH_RELATED_FIELDS
)
Expand Down

0 comments on commit f39f23c

Please sign in to comment.