Django整合许多领域

时间:2018-09-06 15:52:32

标签: django

您好,我想汇总文章中的所有评论,并仅选择此评论中的2条评论,而不是全部评论,并放回到文章var中

    from django.shortcuts import render_to_response
    from django.db.models import BooleanField, Count, OuterRef, Subquery
    from ..models import Article, Like, Bookmark, Comment


    def feed(request):
        context = {}
        articles = Article.objects.all()[:5].select_related('user__profil')\
        .prefetch_related('comments__user__profil', 'hashtags')\
        .annotate(like_count=Count("likes__sum_rating()"), \
                  liked=Subquery(Like.objects.filter(user=request.user, content_type=7, object_id=OuterRef('pk')).annotate(cnt=Count('pk')).values('cnt'),
                                 output_field=BooleanField()),
                  bookmarked=Subquery(Bookmark.objects.filter(user=request.user, article=OuterRef('pk')).annotate(cnt=Count('pk')).values('cnt'),
                              output_field=BooleanField()),
        )

        ## Here my try 
        articles.comments = articles.values_list('comments').annotate(


           izi=Count("comments__likes__sum_rating()")

        )



        context['activate'] = "feed"
        context['articles'] = articles
        return render_to_response(template_name='feed.html', context=context)

0 个答案:

没有答案