单个查询中有两个注释 [英] Two annotations in a single query

查看:66
本文介绍了单个查询中有两个注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个查询集以进行计数,该查询集涉及三个模型。

I am trying to build a query set to do a count, the query set involves three models.

class Owner(models.Model ):
名称= models.CharField(max_length = 10,null = False)

类位置(models.Model):
名称= models.CharField(max_length = 10,null = False)
owner = models.ForeignKey(Owner,on_delete = models.SET_NULL,null = True,blank = True)

class Asset(models.Model):
name = models.CharField(max_length = 10, null = false)
owner = models.ForeignKey(Owner,on_delete = models.SET_NULL,null = True,blank = True)
location = models.ForeignKey(Location,on_delete = models.SET_NULL,null =正确,空白=正确)

我正在尝试计算所有者的位置和所有者的资产,我可以实现这一点作为两个单独的查询集,如下所示:

I am trying to do a count for Locations for the owner and Assets for the owner, I can achieve this as two separate Querysets as follows:

locations = Owner.objects.all()。annotate(locations = Count('location'))

资产= Owner.objects.all()。annotate(assets = Count('asset'))

这很好用,但是我想做的是获取两个值的一行并建立一个类似于下面的表。

This works fine, but what I'm trying to do is get a single row for both values and build a table similar to the one below.

|业主|资产|位置|
| -------- + -------- + ----------- |
| owner1 | 10 | 3 |
| owner2 | 100 | 20 |
| owner3 | 70 | 50 |

我试图将两个注释都放在一个查询中,但似乎没有得到正确的结果,计数在资产和位置上都是相同的,或者我得到的数字非常大,因为两种计数操作相互影响。

I've tried to put both annotations in a single query but I don't appear to get the right results, the count is either the same for both assets and location or I get very large numbers which I assume because both count operations are impacting each other.

以下查询为我提供了相同的数字资产和位置

Query below gives me the same numbers for both asset and location

queryset = Owner.objects.all()。annotate(assets = Count('asset'),location = Count( '位置'))

下面的查询为我提供了大量资产和位置

Query below gives me large numbers for both asset and location

queryset = Owner.objects.all()。annotate(assets = Count('asset'))。annotate(locations = Count('location'))

我可以直接使用SQL进行此操作,但我希望不要沿着那条路走。 / p>

I can do this directly with SQL but I'm hoping not do go down that path.

推荐答案

感谢奈良,我尝试对您提出的建议进行不同的排列,最终使之奏效。

Thanks Nara, I tried a different permutation of what you suggested which ended up working.

queryset = Owner.o bjects.all()。annotate(资产=计数('asset',distinct = True),位置= Count('location',distinct = True))

这是Django shell中的外观。

Here is what it looks like from a Django shell.

>>> from inventory.models import *
>>> from django.db.models import Count
>>> queryset = Owner.objects.all().annotate(assets=Count('asset', distinct=True), locations=Count('location', distinct=True))
>>> vars(queryset[0])
{'_state': <django.db.models.base.ModelState object at 0x0427B230>, 'id': 1, 'name': 'Owner 1', 'assets': 5, 'locations': 4}
>>> vars(queryset[1])
{'_state': <django.db.models.base.ModelState object at 0x0427B2D0>, 'id': 2, 'name': 'Owner 2', 'assets': 3, 'locations': 4}
>>> vars(queryset[2])
{'_state': <django.db.models.base.ModelState object at 0x0427B4D0>, 'id': 3, 'name': 'Owner 3', 'assets': 2, 'locations': 6}

如您所见,这既给了我资产,又给了我位置计数。

As you can see that gives me both the asset and the location counts.

这篇关于单个查询中有两个注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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