How to filter on calculated model values ​​using list comprehension

I have a model where I calculate the totals

class Material(models.Model):
    version = IntegerVersionField( )
    code = models.CharField(max_length=30)
    name = models.CharField(max_length=30)
    slug = models.SlugField(max_length=80, blank=True)
    description = models.TextField(null=True, blank=True)
    min_quantity = models.DecimalField(max_digits=19, decimal_places=10)

    def _get_totalinventory(self):
        from inventory.models import Inventory
        return Inventory.objects.filter(warehouse_Bin__material_UOM__UOM__material=self.id, is_active = True).aggregate(Sum('quantity'))

    total_inventory = property(_get_totalinventory)

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

    total_po = property(_get_totalpo)


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

    total_so = property(_get_totalso)

So far so good, my calculation fields are correctly calculated and displayed in the template without any problems.

Since I cannot filter the property directly. As suggested, I am using python list comprehension

So, this is what I wrote, that gives me the result and no errors.

    po_list = [n for n in Material.objects.all() 

if ((F('total_inventory') + F('total_po') - F('total_so')) < F('min_quantity'))]

However, understanding the current list does not filter correctly. I get output that should be filtered. What am I doing wrong?

It does not filter anything, it just returns a list a

0
source share
1 answer

, F . :

po_list = [n for n in Material.objects.all() if (n.total_inventory['quantity__sum'] 
                                                 + n.total_po['quantity__sum'] 
                                                 - n.total_so['quantity__sum']) 
                                                 < n.min_quantity]
+1

All Articles