[Answered ]-Django: Group By clarification

2👍

Below is the query for group by unread.objects.filter(post=p.id).values(message_unread.id).order_by().annotate(Count('post'))

>>> from django.db.models import Count
>>> m=Unread.objects.filter(post__id='1').values('id').order_by().annotate(Count('post'))
>>> m
[{'id': 1L, 'post__count': 1}]
>>> m.values()
[{'marked_read_on': datetime.datetime(2011, 3, 3, 22, 3, 33), 'user_id': 1L, 'comment_id': 1L, 'post_id': 1L, 'marked_unread_on': datetime.datetime(2011, 3, 3, 22, 3, 29), 'category_id': 1L, 'id': 1L, 'post__count': 1}]

below query

>>> print m.query
SELECT `mytest_unread`.`id`, COUNT(`mytest_unread`.`post_id`) AS `post__count` FROM `mytest_unread` WHERE `mytest_unread`.`post_id` = 1  GROUP BY `mytest_unread`.`id`, `mytest_unread`.`id` ORDER BY NULL
👤sush

Leave a comment