def get(self, request, *args, **kwargs):
box_office = BoxOfficeMovie.objects.all().order_by('-created')[:10]
# ????? ?? ?? ?? ????
comments = []
for i in box_office:
comment = Comment.objects.filter(movie__pk=i.movie.pk)
for k in comment:
comments.append(k)
# print('???', comments)
# 5? ??? 1? ?? ??
comments = sorted(comments, key=attrgetter('likes_count'), reverse=True)
comments = comments[:5]
# print('???', comments)
if len(comments) == 0:
raise NotAcceptable('???? ????')
else:
best_comment = random.sample(comments, 1)
serializer = CommentSerializer(best_comment, many=True)
return Response(serializer.data)
评论列表
文章目录