如何将元组解析为几个属性?

时间:2016-06-12 18:33:55

标签: python django

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

但我不知道 -

  

TypeError:'property'对象不可迭代

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个属性?

1 个答案:

答案 0 :(得分:1)

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

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