姜戈:分组依据?

发布于 2024-08-25 03:59:07 字数 451 浏览 6 评论 0 原文

我正在寻找类似以下内容的内容:

previous_invoices = Invoice.objects.filter(is_open=False)
                                   .order_by('-created')
                                   .group_by('user')

但是 group_by() 不存在...

这将为每个用户找到最近关闭的发票。

这个 聚合 API 似乎可以让你做这样的事情来计数和总和,但我不需要计数或总和或任何东西,我实际上想要发票对象!

I'm looking for something like the following:

previous_invoices = Invoice.objects.filter(is_open=False)
                                   .order_by('-created')
                                   .group_by('user')

but group_by() doesn't exist...

This would find the most recently closed invoice for each user.

This aggregation API seems to let you do stuff like this for counts and sums, but I don't need a count or sum or anything, I actually want the invoice objects!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

十六岁半 2024-09-01 03:59:07

选项 1:

虽然 Django 中不存在 group_by(),但您可以尝试利用 latest() 方法和过滤器用户也是如此:

previous_invoices = Invoice.objects.filter(user=my_user, is_open=False)
                                   .latest('created') 

对于旧版本的 Django,latest() 不存在,查询将如下所示:

previous_invoices = Invoice.objects.filter(user=my_user, is_open=False)
                                   .order_by('-created')[0]

选项 2:

更新:从那时起我就这样做了此处添加了问答样式示例: 如何在 Django ORM 中执行 GROUP BY ... COUNT 或 SUM?,展示如何在 Django ORM 上模拟 GROUP BY 操作。< /em>

如果您绝对想创建 group_by 的效果,那么您可以手动创建一个,如此处接受的答案所示:Django GROUP BY 字段值

  1. 使用 .values_list( )flat=True 来获取数据库中现有值的列表(如果您事先不知道它们)。还可以使用 .distinct() 来消除重复值,因为我们不关心这些值:

    value_list = MyModel.objects.values_list(
        'interesting_field', flat=True
    )。清楚的()
    
  2. 现在迭代 value_list 并填充您的字典:

    group_by_value = {}
    对于 value_list 中的值:
        group_by_value[值] = MyModel.objects.filter(interesting_field=值)
    

现在 group_by_value 字典包含不同值作为键
在您的 interesting_field 中并作为查询集对象的值,每个
包含带有 interesting_field=a 值的 MyModel 条目
来自 value_list


注意:

存在这个库django-group-by 声称在 Django 上添加了 group_by() ,您可能需要检查一下。

Option 1:

Although a group_by() does not exist in Django, you can try and work around on retrieving the most recently closed invoice for each user by utilizing latest() method and filter for a user as well:

previous_invoices = Invoice.objects.filter(user=my_user, is_open=False)
                                   .latest('created') 

For older versions of Django were latest() does not exist, the query will look like this:

previous_invoices = Invoice.objects.filter(user=my_user, is_open=False)
                                   .order_by('-created')[0]

Option 2:

UPDATE: I have since then added a Q&A style example here: How to execute a GROUP BY ... COUNT or SUM in Django ORM? that shows how to simulate a GROUP BY operation on Django ORM.

If you absolutely want to create the effects of a group_by, then you can create one manually as shown in the accepted answer here: Django GROUP BY field value.

  1. Use .values_list() with flat=True to get a list of the existent values in your database (if you don't know them beforehand). Also use .distinct() to eliminate duplicate values as we do not care for those:

    value_list = MyModel.objects.values_list(
        'interesting_field', flat=True
    ).distinct()
    
  2. Now iterate through value_list and fill your dictionary:

    group_by_value = {}
    for value in value_list:
        group_by_value[value] = MyModel.objects.filter(interesting_field=value)
    

Now group_by_value dictionary contains as keys the distinct values
in your interesting_field and as values the queryset objects, each
containing the entries of MyModel with interesting_field=a value
from value_list
.


Note:

There exists this library django-group-by which claims to add a group_by() on Django you may want to check.

你げ笑在眉眼 2024-09-01 03:59:07

此页面来自 2007 年谁将其破解到了 django,但这毫无意义,因为 1.1 之前的版本确实有一个未记录的 group_by`。但一年前的这条帖子似乎有一些见解。本质上:

Django 故意不公开“GROUP BY”或类似的内容,
ORM。尽管事实上我们是
通过关系存储后端
有时会泄漏 ORM API
与 SQL 相当不可知。相反,我们
暴露特定的部分
恰好是的功能
使用“GROUP BY”实现时
变成 SQL(很可能是
由一些完全不同的人实施
一组神奇的仙女
不同的存储系统后端)。

另请参阅使用 extra 的提及,以及可能失败的神奇方式。祝你好运。

There's this page from 2007 who hacked it to django, but that would be moot since pre 1.1 does have an undocumented group_by` anyway. But this thread from a year ago seems to have some insights. Essentially:

Django intentionally does not expose "GROUP BY" or anything like that, in
the ORM. Although the fact that we're
over a relational storage backend
sometimes leaks through, the ORM API
is fairly SQL-agnostic. Instead, we
expose particular pieces of
functionality that happen to be
implemented using "GROUP BY" when it's
turned into SQL (and could well be
implemented by some entirely different
set of magical fairies with a
different storage system backend).

See also the mentions of using extra, and also the magical ways in which that can fail. Best of luck.

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