如何在sqlalchemy中聚合距离

How to aggregate distance in sqlalchemy

我有以下查询

query = Studio.query.join(
        Location
    ).filter(
        func.ST_DWithin(Location.point, point, distance)
    ).order_by(
        func.ST_Distance(Location.point, point).label('distance')
    )

如何聚合属性的距离?例如:

query[0].distance

query[0].location.distance

最后我使用生成器注入 属性

def smart_generator(query, column_name):
    for obj, additional_data in query.all():
        setattr(obj, column_name, additional_data)
        yield obj


distance_query = StudioLocation.point.ST_Distance(point).label('distance')
nearby_query = Location.point.ST_DWithin(point, distance)
studio_query = db.session.query(Studio, distance_query).join(Location)
studio_query = studio_query.filter(nearby_query).order_by('distance')
studio = smart_generator(studio_query, "distance")