Я бы хотел получить такой эффект
SELECT "restApi_comment"."id", "restApi_comment"."text", "restApi_comment"."movie_id", COUNT("restApi_comment"."id") AS "count" FROM "restApi_comment" GROUP BY "restApi_comment"."movie_id" ORDER BY "count" DESC
, но Django основан на этом
obj = Comment.objects.annotate(count=Count('movie_id')).order_by('-count')
дай мне
SELECT "restApi_comment"."id", "restApi_comment"."text", "restApi_comment"."movie_id", COUNT("restApi_comment"."id") AS "count" FROM "restApi_comment" GROUP BY "restApi_comment"."id", "restApi_comment"."text", "restApi_comment"."movie_id" ORDER BY "count" DESC
проблема заключается в группировке, основанной не на одном столбце (movie_id), а на трех (id, text, movie_id).
[models.py]
class Comment(models.Model):
text = models.CharField(max_length=50, blank=True)
movie = models.ForeignKey(Movie, on_delete=models.CASCADE)
Я попробовал этот метод
obj=Comment.objects.values('movie_id').annotate(count=Count('movie_id')).order_by('-count').values('movie_id', 'text')
, который дал мне такой эффект и не вернул все столбцы
SELECT "restApi_comment"."movie_id", COUNT("restApi_comment"."movie_id") AS "count" FROM "restApi_comment" GROUP BY "restApi_comment"."movie_id" ORDER BY "count" DESC