简体   繁体   中英

How to get the difference of 2 different django models fields?

I have here 2 django database table

class ProductName(models.Model):
     name = models.CharField(max_length=100)

class Inventory(models.Model):
     product = models.ForeignKey(ProductName, on_delete = models.CASCADE )
     qty = models.PositiveIntegerField()

class Sold(models.Model):
     product = models.ForeignKey(ProductName, on_delete = models.CASCADE )
     qty = models.PositiveIntegerField()

I would like to have an inventory table page wherein I can see the total qty left in Inventory (for example: Inventory.qty - Sold.qty) .

how to do this in Django ?

In your views file

from .model import Inventory, Sold

qty_left = Inventory.objects.count() - Sold.objects.count()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM