在django视图中按降序排序

在django视图中按降序排序,django,django-models,django-forms,django-views,Django,Django Models,Django Forms,Django Views,我正在使用 sorted() DatePosted 但我想把它按相反的顺序排序,也就是说,从新到老。我怎么做 def feed(request): if request.user.is_authenticated: result_list=post.objects.none() usrpost=post.objects.none() channel_result=channel.objects.none() chnpost=channel.objects.non

我正在使用
sorted()

DatePosted
但我想把它按相反的顺序排序,也就是说,从新到老。我怎么做

def feed(request):
if request.user.is_authenticated:
    result_list=post.objects.none()
    usrpost=post.objects.none()
    channel_result=channel.objects.none()
    chnpost=channel.objects.none()
    userprofile=FollowingProfiles.objects.filter(Profile__user=request.user)
    for p in userprofile:
        postuser=post.objects.filter(Profile__user__username=p.ProfileName)
        result_list=sorted(chain(usrpost,postuser),key=operator.attrgetter('DatePosted'))
        usrpost=result_list
        result_list=usrpost
        postuser=post.objects.none()
    profile_result=Profile.objects.filter(user__username=request.user.username)

只需添加
reverse=True

sorted(chain(usrpost,postuser),key=operator.attrgetter('DatePosted'), reverse=True)
但我相信您可以做得更好,因为在您的方法中,您可以执行许多SQL查询

更像

userprofile_usernames=FollowingProfiles.objects.filter(Profile__user=request.user).values_list('ProfileName', flat=True)
post.objects.filter(Profile__user__username__in=userprofile_usernames).order_by('DatePosted')
或者将
DatePosted
更改为
-DatePosted
以反转