此视图有效,但是注释注释未在模板中显示为{{ comment.like_count }}
;但它在django-debug-toolbar
中显示var。
def feed(request):
context = {}
articles = Article.objects.select_related('user__profil')\
.prefetch_related('comments__user__profil', 'hashtags', 'comments__likes')\
.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()),
)[:5]
for a in articles:
comments = Comment.objects.filter(articles=a.id)\
.annotate(
like_count=Count("likes__sum_rating()"),
liked=Subquery(
Like.objects.filter(user=request.user, content_type=9, object_id=OuterRef('pk'))\
.annotate(cnt=Count('pk')).values('cnt'),
output_field=BooleanField()),
test=Count(1)
)[:2]
a.comments.set(comments)
context['activate'] = "feed"
context['articles'] = articles
return render_to_response(template_name='feed.html', context=context)
工具栏: