gpt4 book ai didi

python - 如何将元组解析为多个属性?

转载 作者:太空宇宙 更新时间:2023-11-03 16:28:47 25 4
gpt4 key购买 nike

在我的 django 应用程序中,我在模型端进行计算,为此我仅使用一个返回元组的函数。我想将这个元组解析为几个属性

但我不能从那以后 -

TypeError: 'property' object is not iterable

def _get_total(self):
from inventory.models import Inventory
inventory_quantity = Inventory.objects.filter(material=self.id, is_active = True ).aggregate(Sum('quantity'))

from purchase.models import POmaterial
po_quantity = POmaterial.objects.filter(material=self.id, is_active = True).aggregate(Sum('quantity'))

from sales.models import SOproduct
so_quantity = SOproduct.objects.filter(product__material=self.id , is_active=True ).aggregate(Sum('quantity'))


actual_quantity = inventory_quantity + po_quantity - so_quantity

min_deficit = self.min_quantity - actual_quantity

max_deficit = self.max_quantity - actual_quantity



return inventory_quantity, po_quantity, so_quantity, min_deficit , max_deficit




total_inventory, total_po, total_so,min_deficit, max_deficit = property(_get_total())

有什么想法可以如何从此元组创建所有 5 个属性吗?

最佳答案

以下代码是否提供了必要的功能?

class MyClass(ClassParent):

_total_inventory = None
_total_po = None
...
...

def _get_total_(self):
...
# everything before the return clause
self._total_inventory = inventory_quantity
self._total_po = po_quantity
...

@property
def total_inventory(self):
if self._total_inventory is None:
self._get_total()
return self._total_inventory

@property
def total_po(self):
if self._total_po is None:
self._get_total()
return self._total_po
...
# And so on

关于python - 如何将元组解析为多个属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37777755/

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