Передать пользовательский аргумент в order_by SqlAlchemy - PullRequest
0 голосов
/ 18 января 2020

Итак, у меня есть модель SQLALchemy, как показано ниже

from sqlalchemy import (create_engine, Column, BigInteger, String, 
                        DateTime)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property

Base = declarative_base()

class SomePoint(Base):
    __tablename__ = 'points'

    point_id = Column(BigInteger, primary_key=True, autoincrement=True)
    demesion_1 = Column(BigInteger)
    demesion_2 = Column(BigInteger)
    demesion_3 = Column(BigInteger)
    demesion_4 = Column(BigInteger)

    def getCoords(self):
        return [self.demesion_1, self.demesion_2, self.demesion_3, self.demesion_4]

    def distanceToPoint(self, otherPoint):
        diff = list(map(operator.sub, self.getCoords(), otherPoint.getCoords()))
        return np.linalg.norm(diff)

    @hybrid_property
    def distance(self, ...):
        #some magic here

    @distance.expression
    def distance(cls, ...):
        # some magic here


Я хочу иметь возможность упорядочить запрос по расстоянию до некоторой контрольной точки (например, someOtherPoint) И я хочу сделать это следующим образом :

session.query(SomePoint).order_by(SomePoint.distance(someOtherPoint).desc())
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...