Django:分组依据说明

发布于 2024-10-20 07:03:19 字数 2836 浏览 2 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你再特殊 2024-10-27 07:03:19

下面是 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}]

查询

>>> 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文