Django:分组依据说明
我有一个views.py,看起来像这样:
category = Category.objects.get(id=categoryid)
#posts = get_list_or_404(Post, category=categoryid)
posts = Post.objects.filter(category=categoryid,is_comment=0).order_by('-published')
all_posts = []
for p in posts:
all_posts += [(x.id, x.marked_read_on, p, \
Unread.objects.filter(marked_read_on__isnull=True, \
user=request.user,comment__post=p.id).count(), \
Unread.objects.filter(user=request.user,comment__post=p.id).count(), \
#1
) \
#for x in p.unread_set.filter(post=p.id)]
for x in p.unread_set.filter(post=p.id).annotate(Count('post')).order_by('post')]
For for x in p.unread_set.filter(post=p.id).annotate(Count('post')).order_by('post')]< /code> 我得到下面的 sql 查询:(没有错误)
SELECT `message_unread`.`id`, `message_unread`.`user_id`, `message_unread`.`post_id`, `message_unread`.`comment_id`, `message_unread`.`category_id`, `message_unread`.`marked_unread_on`, `message_unread`.`marked_read_on`, COUNT(`message_unread`.`post_id`) AS `post__count` FROM `message_unread` WHERE (`message_unread`.`post_id` = 4 AND `message_unread`.`post_id` = 4 ) GROUP BY `message_unread`.`id` ORDER BY `message_unread`.`post_id` ASC
我只想要这部分: GROUP BY
message_unread
.id
变为: GROUP BY
message_unread
.post_id
我只想按 post_id 对结果进行分组。仅供参考,应用程序的名称是“message”,这就是查询具有“message_”前缀的原因。
我该怎么做呢?
更新,我的 models.py 如下所示:
class Category(models.Model):
name = models.CharField(max_length=120)
group = models.ForeignKey(Group)
def __unicode__(self):
return self.name
class Post(models.Model):
title = models.CharField(max_length=250)
#slug = models.SlugField(max_length=250, unique=True)
body = models.TextField()
published = models.DateTimeField(default=datetime.now)
category = models.ForeignKey(Category)
group = models.ForeignKey(Group)
user = models.ForeignKey(User)
is_comment = models.BooleanField(default=0)
def __unicode__(self):
return self.title
class Comment(models.Model):
post = models.ForeignKey(Post, related_name='comment_parent')
comment = models.ForeignKey(Post)
def __unicode__(self):
return comment.title
class Unread(models.Model):
user = models.ForeignKey(User)
post = models.ForeignKey(Post)
comment = models.ForeignKey(Comment, null=True, blank=True)
category = models.ForeignKey(Category)
marked_unread_on = models.DateTimeField(null=True, blank=True)
marked_read_on = models.DateTimeField(null=True, blank=True)
I have a views.py that looks something like this:
category = Category.objects.get(id=categoryid)
#posts = get_list_or_404(Post, category=categoryid)
posts = Post.objects.filter(category=categoryid,is_comment=0).order_by('-published')
all_posts = []
for p in posts:
all_posts += [(x.id, x.marked_read_on, p, \
Unread.objects.filter(marked_read_on__isnull=True, \
user=request.user,comment__post=p.id).count(), \
Unread.objects.filter(user=request.user,comment__post=p.id).count(), \
#1
) \
#for x in p.unread_set.filter(post=p.id)]
for x in p.unread_set.filter(post=p.id).annotate(Count('post')).order_by('post')]
For for x in p.unread_set.filter(post=p.id).annotate(Count('post')).order_by('post')]
I get the sql query below: (no errors)
SELECT `message_unread`.`id`, `message_unread`.`user_id`, `message_unread`.`post_id`, `message_unread`.`comment_id`, `message_unread`.`category_id`, `message_unread`.`marked_unread_on`, `message_unread`.`marked_read_on`, COUNT(`message_unread`.`post_id`) AS `post__count` FROM `message_unread` WHERE (`message_unread`.`post_id` = 4 AND `message_unread`.`post_id` = 4 ) GROUP BY `message_unread`.`id` ORDER BY `message_unread`.`post_id` ASC
I just want this part:GROUP BY
message_unread
.id
To become:GROUP BY
message_unread
.post_id
I just want to group my results by post_id. FYI, the name of the app is "message" that is why the query has a "message_" prefix.
How would I do that?
Update, my models.py looks like this:
class Category(models.Model):
name = models.CharField(max_length=120)
group = models.ForeignKey(Group)
def __unicode__(self):
return self.name
class Post(models.Model):
title = models.CharField(max_length=250)
#slug = models.SlugField(max_length=250, unique=True)
body = models.TextField()
published = models.DateTimeField(default=datetime.now)
category = models.ForeignKey(Category)
group = models.ForeignKey(Group)
user = models.ForeignKey(User)
is_comment = models.BooleanField(default=0)
def __unicode__(self):
return self.title
class Comment(models.Model):
post = models.ForeignKey(Post, related_name='comment_parent')
comment = models.ForeignKey(Post)
def __unicode__(self):
return comment.title
class Unread(models.Model):
user = models.ForeignKey(User)
post = models.ForeignKey(Post)
comment = models.ForeignKey(Comment, null=True, blank=True)
category = models.ForeignKey(Category)
marked_unread_on = models.DateTimeField(null=True, blank=True)
marked_read_on = models.DateTimeField(null=True, blank=True)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面是 group by
unread.objects.filter(post=p.id).values(message_unread.id).order_by().annotate(Count('post'))
的查询
Below is the query for group by
unread.objects.filter(post=p.id).values(message_unread.id).order_by().annotate(Count('post'))
below query