gpt4 book ai didi

python - Flask 管理员 - 对 hybrid_property 进行复杂排序

转载 作者:行者123 更新时间:2023-12-05 04:24:00 25 4
gpt4 key购买 nike

我有一个相当复杂的 hybrid_property。这是一个供应商模型,它有多个 skuchannels(产品)。它的作用是:根据 target_stock_duration(例如,我们希望将商品存货 4 个月)计算必须订购的单位数量以及这将花费多少。这给了我们潜力。

class Vendor(db.Model):
__tablename__ = "vendor"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(150))
b2c_price_factor = db.Column(db.Float, nullable=False)
skuchannels = db.relationship("SKUChannel", back_populates="vendor")

@hybrid_property
def po_potential(self):
"""This is a "virtual" property that will that can be used in the admin view. It calculates
the potential value for a comming PO.

Returns:
_type_: _description_
"""

potential = 0
for item in self.skuchannels:
purchasing_price = item.purchasing_price if item.purchasing_price != None else 0

target_stock_duration = 4

try:
to_order = item.average_monthly_sales * target_stock_duration - item.stock_level #calculate how many units we have to order
if to_order < 0:
to_order = 0
except TypeError:
to_order = 0

potential = potential + purchasing_price * to_order #calculate how much everything costs

return potential

好吧,这个 hybrid_property 工作得很好,但我非常想对这个属性进行排序。使用@po_potential.expression -> 好吧,我不知道该怎么做,因为据我所知,它应该返回一个选择对象。还有其他办法吗?

最佳答案

这应该让你开始:

class Vendor(Base):
...
...
@po_potential.expression
def po_potential(cls):
target_stock_duration = 4
return (
select(func.sum(
func.ISNULL(SKUChannel.purchasing_price, 0) *
func.GREATEST(0, SKUChannel.average_monthly_sales * target_stock_duration - SKUChannel.stock_level, 0)
))
.where(SKUChannel.vendor_id == cls.id)
.label('po_potential')
)

关于python - Flask 管理员 - 对 hybrid_property 进行复杂排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73575168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com