1

I have two Models, one for Intakes of Goods in the warehouse, the other for Output of goods. My available inventory per item is the sum of the intakes minus the sum of the outputs for each item. How can I do that in a query in Django?

I have tried using the Django ORM but apparently its only for joining models with dependencies among each other.

THIS IS THE MODEL FOR THE OUTPUT OF GOODS. ITS LOCATED IN THE APP, LETS SAY "1" OF MY PROJECT.

class OutputPosition(models.Model):

created_at = models.DateTimeField(auto_now=True, null=False)
output = models.ForeignKey(Output, related_name='outputs', on_delete=models.CASCADE)
item = models.ForeignKey(Item, related_name='output_items', help_text='Nombre del artículo', on_delete=models.SET_DEFAULT, default=999999999)
quantity = models.SmallIntegerField(null=False, help_text='Ingrese el número de artículos')
comments = models.CharField(max_length=256, null=True, help_text='Comentarios')

def get_item_name(self):
    return self.item.name

def __str__(self):
    return str(self.pk)+' '+self.item.name

THIS ONE BELOW IS THE MODEL FOR THE INTAKE OF GOODS. IS LOCATED IN ANOTHER APP.

class Positions(models.Model):

created_at = models.DateTimeField(auto_now=True, null=False)
intake = models.ForeignKey(Intake, related_name='intakes', on_delete=models.CASCADE)
item = models.ForeignKey(Item, related_name='intake_items', help_text='Nombre del artículo', on_delete=models.SET_DEFAULT, default=999999999)
quantity = models.SmallIntegerField(null=False, help_text='Ingrese el número de artículos')
comments = models.CharField(max_length=256, null=True, help_text='Comentarios')

def get_item_name(self):
    return self.item.name

def __str__(self):
    return str(self.pk)+' '+self.item.name

class Meta:
    verbose_name_plural = "Positions"

I have one view which I want to use to calculate the sum of all intakes - the sum of all outputs for each item and display it in an html table.

orcaen7
  • 51
  • 5

0 Answers0