hybrid_expressions 在带参数的 SQLAlchemy 中
hybrid_expressions in SQLAlchemy with arguments
我目前在我概述的模型上有以下 hybrid_property
和 hybrid_property.expression()
:
class Body():
@hybrid_property
def altitude(self, LST: float, latitude: float) -> float:
return self.get_altitude(LST, latitude)
@altitude.expression
def altitude(cls, LST: float, latitude: float) -> float:
"""
Get the altitude of the star as a raw SQL expressions
"""
ha = (LST - cls.ra) % 360
ra = func.radians(ha)
dec = func.radians(cls.dec)
lat = func.radians(latitude)
alt = func.asin(
func.sin(dec) * func.sin(lat) + func.cos(dec) * func.cos(lat) * func.cos(ra)
)
return func.degrees(alt)
但是,当我尝试在 SQLAlchemy 过滤器中使用它时:
query = query.filter(self.model.altitude(LST, latitude) > 0)
我收到以下错误:
TypeError: altitude() missing 2 required positional arguments: 'LST' and 'latitude'
即使它们都已定义。
您正在查找 hybrid_method
,hybrid_property
不支持参数。
class Body():
@hybrid_method
def altitude(self, LST: float, latitude: float) -> float:
return self.get_altitude(LST, latitude)
@altitude.expression
def altitude(self, LST: float, latitude: float) -> float:
"""
Get the altitude of the star as a raw SQL expressions
"""
ha = (LST - cls.ra) % 360
ra = func.radians(ha)
dec = func.radians(cls.dec)
lat = func.radians(latitude)
alt = func.asin(
func.sin(dec) * func.sin(lat) + func.cos(dec) * func.cos(lat) * func.cos(ra)
)
return func.degrees(alt)
我目前在我概述的模型上有以下 hybrid_property
和 hybrid_property.expression()
:
class Body():
@hybrid_property
def altitude(self, LST: float, latitude: float) -> float:
return self.get_altitude(LST, latitude)
@altitude.expression
def altitude(cls, LST: float, latitude: float) -> float:
"""
Get the altitude of the star as a raw SQL expressions
"""
ha = (LST - cls.ra) % 360
ra = func.radians(ha)
dec = func.radians(cls.dec)
lat = func.radians(latitude)
alt = func.asin(
func.sin(dec) * func.sin(lat) + func.cos(dec) * func.cos(lat) * func.cos(ra)
)
return func.degrees(alt)
但是,当我尝试在 SQLAlchemy 过滤器中使用它时:
query = query.filter(self.model.altitude(LST, latitude) > 0)
我收到以下错误:
TypeError: altitude() missing 2 required positional arguments: 'LST' and 'latitude'
即使它们都已定义。
您正在查找 hybrid_method
,hybrid_property
不支持参数。
class Body():
@hybrid_method
def altitude(self, LST: float, latitude: float) -> float:
return self.get_altitude(LST, latitude)
@altitude.expression
def altitude(self, LST: float, latitude: float) -> float:
"""
Get the altitude of the star as a raw SQL expressions
"""
ha = (LST - cls.ra) % 360
ra = func.radians(ha)
dec = func.radians(cls.dec)
lat = func.radians(latitude)
alt = func.asin(
func.sin(dec) * func.sin(lat) + func.cos(dec) * func.cos(lat) * func.cos(ra)
)
return func.degrees(alt)