Django:子查询注释 [英] Django: Annotation on Subquery

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

问题描述

我正在尝试使用Django 2.0.3和PostGIS用最近的相邻 Station id 注释 Station 的查询集(GeoDjango)功能.

I'm trying to annotate a queryset of Stations with the id of the nearest neighbouring Station using Django 2.0.3 and PostGIS (GeoDjango) functions.

简化的 Station 型号:

class Station(models.Model):
    name = models.CharField(max_length=128)
    location = models.PointField()
    objects = StationQuerySet.as_manager()

我遇到的问题是试图计算最接近的距离,这涉及到注释一个子查询,该子查询引用外部查询集中的 location .

The problem I'm having is trying to compute the closest distance, which involves annotating a subquery which refers to the location in the outer queryset.

from django.db.models import OuterRef, Subquery
from django.contrib.gis.db.models.functions import Distance

class StationQuerySet(models.QuerySet):

    def add_nearest_neighbour(self):
        '''
        Annotates each station with the id and distance of the nearest neighbouring station
        '''
        # Get Station model
        Station = self.model

        # Calculate distances to each station in subquery
        subquery_with_distance = Station.objects.annotate(distance=Distance('location', OuterRef('location')) / 1000)

        # Get nearest from subquery
        nearest = subquery_with_distance.order_by('distance').values('id')[0]

        return self.annotate(
            nearest_station_id=Subquery(nearest)
        )

distance = Station.objects.annotate(distance = Distance('location',OuterRef('location'))/1000)行导致以下错误:

from apps.bikeshare.models import Station
stations = Station.objects.add_nearest_neighbour()

错误:

Traceback (most recent call last):
  File "/home/gbrown/Envs/bikeshare-dev/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2847, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-3-cb35ea6d5d8b>", line 1, in <module>
    stations = Station.objects.add_nearest_neighbour()
  File "/home/gbrown/Envs/bikeshare-dev/lib/python3.5/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/gbrown/Development/transit_bikeshare/apps/bikeshare/querysets.py", line 162, in add_nearest_neighbour
    subquery_with_distance = Station.objects.annotate(distance=Distance('location', OuterRef('location')) / 1000)
  File "/home/gbrown/Envs/bikeshare-dev/lib/python3.5/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/gbrown/Envs/bikeshare-dev/lib/python3.5/site-packages/django/db/models/query.py", line 997, in annotate
    clone.query.add_annotation(annotation, alias, is_summary=False)
  File "/home/gbrown/Envs/bikeshare-dev/lib/python3.5/site-packages/django/db/models/sql/query.py", line 975, in add_annotation
    summarize=is_summary)
  File "/home/gbrown/Envs/bikeshare-dev/lib/python3.5/site-packages/django/db/models/expressions.py", line 452, in resolve_expression
    c.lhs = c.lhs.resolve_expression(query, allow_joins, reuse, summarize, for_save)
  File "/home/gbrown/Envs/bikeshare-dev/lib/python3.5/site-packages/django/contrib/gis/db/models/functions.py", line 58, in resolve_expression
    source_fields = res.get_source_fields()
  File "/home/gbrown/Envs/bikeshare-dev/lib/python3.5/site-packages/django/db/models/expressions.py", line 349, in get_source_fields
    return [e._output_field_or_none for e in self.get_source_expressions()]
  File "/home/gbrown/Envs/bikeshare-dev/lib/python3.5/site-packages/django/db/models/expressions.py", line 349, in <listcomp>
    return [e._output_field_or_none for e in self.get_source_expressions()]
AttributeError: 'ResolvedOuterRef' object has no attribute '_output_field_or_none'

推荐答案

使用原始查询找到最近的电台并选择子查询的ID AND距离,以下是额外的解释:

Came up with a work around using a raw query to find the nearest station and select the id AND distance from the subquery, bonus explanation below:

class StationQuerySet(models.QuerySet):

    def nearest_neighbour(self):
        '''
        Creates a RawQuerySet of each station with the id and distance of the nearest neighbouring station
        '''
        # Have to execute the query in order to get the list of ids to inject
        ids = tuple(self.values('id').values_list('id', flat=True))

        return self.raw('''
               SELECT
                 A0.id   as id,
                 SUB.closest_id,
                 SUB.closest_distance
               FROM "bikeshare_station" A0
                 CROSS JOIN LATERAL (
                            SELECT
                              B0.id   as closest_id,
                              st_distance_sphere(A0.location, B0.location) as closest_distance
                            FROM "bikeshare_station" B0
                            WHERE A0.id != B0.id
                            ORDER BY A0.location <-> B0.location
                            limit 1
                            ) SUB
               WHERE A0.id IN %s;
           ''', [ids])

用法

您可以将查询集调用链接在一起,以在查找最近的邻居之前过滤查询集:

Usage

You can chain querysets calls together to filter down the queryset before finding the nearest neighbour:

query = Station.objects.filter(name='Albert Gate, Hyde Park')
closest_stations = query.nearest_neighbour()
station = closest_stations[0]
station.name
[out]: 'Albert Gate, Hyde Park'
station.closest_distance
[out]: 133.52459069
station.closest_id
[out]: 6369

SQL解释

这种类型的子查询称为相关子查询,因为它引用外部查询中的列.此外,我需要选择有关最近车站的多条信息( id 距离等).

子查询放置在 FROM 子句中,该子查询允许选择多列.需要 LATERAL 连接,以允许子查询引用 FROM 列表中的兄弟表.在子查询返回单行的情况下,可以使用 CROSS 联接基于笛卡尔乘积而不是共享列来形成联接表.

The subquery is placed in the FROM clause, which allows multiple columns to be selected. A LATERAL join is needed to allow the subquery to reference the sibling table in the FROM list. With the subquery returning a single row, a CROSS join can be applied to form a joined table based on the cartesian product rather than on a shared column.

子查询使用PostGIS <-> 运算符,该运算符通过按站点之间的距离和 st_distance_sphere 排序表的效率更高.点之间的精确距离计算.

The subquery uses the PostGIS <-> operator, which is much more efficient at ordering the table by distance between the stations, and st_distance_sphere, to do an accurate distance calculation between the points.

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

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