Ilya Bibik
Ilya Bibik

Reputation: 4124

How to parse tuple into several properties?

In my django app I do calculations on model side, for this i use only one function that returns a tuple. I want to parse this tuple into several properties

But I cant since -

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())

any ideas how can I create all 5 properties from this tuple?

Upvotes: 1

Views: 91

Answers (1)

raphv
raphv

Reputation: 1183

does the following code provide the necessary functionality?

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

Upvotes: 1

Related Questions