从不同类别获取最新对象的Django查询 [英] Django Query That Get Most Recent Objects From Different Categories

查看:74
本文介绍了从不同类别获取最新对象的Django查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型 A B 。所有 B 对象具有 A 对象的外键。给定一组 A 对象,是否仍然使用ORM来获取一组包含最多的 B 对象为每个 A 对象创建的最近对象



以下是一个简化示例:

 类面包店(models.Model):
town = models.CharField()

类蛋糕(models.Model):
面包店= models.ForeignKey(面包店)
baked_at = models.DateTimeField()

所以我正在寻找一个查询,返回在美国Anytown的每个面包店烘烤的最新蛋糕。

解决方案

据我所知,在Django ORM中没有一步做到这一点。



但是您可以将其分为两个查询:

  bakeries = Bakery.objects.annotate(hottest_cake_baked_at = Max('cake__baked_at'))
hottest_cakes = Cake.objects.filter(baked_at__in = [b .hottest_cake_baked_at for b in bakeries])

如果id的蛋糕与bake_at时间戳一起进行,您可以简化和消除上述代码(如果两个蛋糕在同一时间可以获得两个蛋糕):

  hottest_cake_ids = Bakery.objects.annotate(hottest_cake_id = Max('cake__id'))values_list('hottest_cak e_id',flat = True)
hottest_cakes = Cake.objects.filter(id__in = hottest_cake_ids)

BTW这笔信用于Daniel Roseman,曾回答过我的类似问题:



http://groups.google.pl/group/django-users/browse_thread/thread/3b3cd4cbad478d34/3e4c87f336696054?hl=pl&q=



如果上述方法太慢,那么我也知道第二种方法 - 您可以编写自定义SQL,只生成相关面包师中最热门的蛋糕,将其定义为d atabase VIEW,然后为其编写非托管的Django模型。在上面的django-users线程中也提到。直接链接到原来的概念在这里:



http://web.archive.org/web/20130203180037/http://wolfram .kriesing.de / blog / index.php / 2007 / django-nice-and-critical-article#comment-48425



希望这有帮助。 / p>

I have two models A and B. All B objects have a foreign key to an A object. Given a set of A objects, is there anyway to use the ORM to get a set of B objects containing the most recent object created for each A object

Here's an simplified example:

Class Bakery(models.Model):
    town = models.CharField()

Class Cake(models.Model):
    bakery = models.ForeignKey(Bakery)
    baked_at = models.DateTimeField()

So I'm looking for a query that returns the most recent cake baked in each bakery in Anytown, USA.

解决方案

As far as I know, there is no one-step way of doing this in Django ORM.

But you can split it in two queries:

bakeries = Bakery.objects.annotate(hottest_cake_baked_at=Max('cake__baked_at')) 
hottest_cakes = Cake.objects.filter(baked_at__in=[b.hottest_cake_baked_at for b in bakeries])

If id's of cakes are progressing along with bake_at timestamps, you can simplify and disambiguate the above code (in case two cakes arrives at the same time you can get both of them):

hottest_cake_ids = Bakery.objects.annotate(hottest_cake_id=Max('cake__id')).values_list('hottest_cak‌​e_id', flat=True)
hottest_cakes = Cake.objects.filter(id__in=hottest_cake_ids)

BTW credits for this goes to Daniel Roseman, who once answered similar question of mine:

http://groups.google.pl/group/django-users/browse_thread/thread/3b3cd4cbad478d34/3e4c87f336696054?hl=pl&q=

If the above method is too slow, then I know also second method - you can write custom SQL producing only those Cakes, that are hottest in relevant Bakeries, define it as database VIEW, and then write unmanaged Django model for it. It's also mentioned in the above django-users thread. Direct link to the original concept is here:

http://web.archive.org/web/20130203180037/http://wolfram.kriesing.de/blog/index.php/2007/django-nice-and-critical-article#comment-48425

Hope this helps.

这篇关于从不同类别获取最新对象的Django查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆