基于相关模型计数的Django过滤

Wes*_*ssi 4 python django django-models python-3.x

我有以下工作代码:

houses_of_agency = House.objects.filter(agency_id=90)
area_list = AreaHouse.objects.filter(house__in=houses_of_agency).values('area')
area_ids = Area.objects.filter(area_id__in=area_list).values_list('area_id', flat=True)
Run Code Online (Sandbox Code Playgroud)

这将返回一个带有 area_ids 列表的查询集。我想进一步过滤,以便只获得属于该机构的 100 多所房屋的 area_ids。

我尝试了以下调整:

houses_of_agency = House.objects.filter(agency_id=90)
area_list = AreaHouse.objects.filter(house__in=houses_of_agency).annotate(num_houses=Count('house_id')).filter(num_houses__gte=100).values('area')
area_ids = Area.objects.filter(area_id__in=area_list).values_list('area_id', flat=True)
Run Code Online (Sandbox Code Playgroud)

但它返回一个空的查询集。

我的模型(简化)如下所示:

class House(TimeStampedModel):
    house_pk = models.IntegerField()
    agency = models.ForeignKey(Agency, on_delete=models.CASCADE)


class AreaHouse(TimeStampedModel):
    area = models.ForeignKey(Area, on_delete=models.CASCADE)
    house = models.ForeignKey(House, on_delete=models.CASCADE)


class Area(TimeStampedModel):
    area_id = models.IntegerField(primary_key=True)
    parent = models.ForeignKey('self', null=True)
    name = models.CharField(null=True, max_length=30)
Run Code Online (Sandbox Code Playgroud)

编辑:我使用 MySQL 作为数据库后端。

oli*_*del 9

agency_id只用一个下划线查询。我在下面更正了您的疑问。此外,在 django 中更常见的是使用pk而不是id行为是相同的。此外,不需要三个单独的查询,因为您可以将所有内容合二为一。

另请注意,您的字段area_idhouse_pk是不必要的,django 会自动创建可通过modelname__pk.

# note how i inlined your first query in the .filter() call
area_list = AreaHouse.objects \
            .filter(house__agency__pk=90) \
            .annotate(num_houses=Count('house')) \  # <- 'house'
            .filter(num_houses__gte=100) \
            .values('area')

# note the double underscore
area_ids = Area.objects.filter(area__in=area_list)\
                       .values_list('area__pk', flat=True)
Run Code Online (Sandbox Code Playgroud)

如果您不需要中间结果,您可以进一步简化它。这是两个查询的组合:

area_ids = AreaHouse.objects \
            .filter(house__agency__pk=90) \
            .annotate(num_houses=Count('house')) \
            .filter(num_houses__gte=100) \
            .values_list('area__pk', flat=True)
Run Code Online (Sandbox Code Playgroud)

最后,您似乎是在模型中手动定义多对多关系(通过AreaHouse)。有更好的方法可以做到这一点,请阅读django 文档

  • 您可能想要检查您的数据。查询看起来正确。 (2认同)