Trying to learn python and django at the same time so forgive me if the question is easy.
I have the following code in my signals.py file
@receiver(post_save, sender=PurchaseOrderProductLine)
def save_inventory_line(sender, instance, **kwargs):
inventory_line = Inventory.objects.filter(product=instance.product, condition=instance.product_condition)
if inventory_line.exists():
inventory_line[0].quantity = inventory_line[0].quantity + instance.quantity
inventory_line[0].save()
else:
Inventory.objects.create(
purchase_order=instance.purchase_order,
quantity=instance.quantity,
condition=instance.product_condition,
product=instance.product
)
If the inventory_line.exists() queryset evaluates to false then the new inventory object is created and added to the database as it should. However if the queryset is true no new inventory object is added. which is good, but the inventory[0] object is not updated in the database.
here is my Inventory model
class Inventory(models.Model):
quantity = models.PositiveIntegerField()
product = models.ForeignKey('Product', on_delete=models.CASCADE)
condition = models.ForeignKey('ProductCondition', on_delete=models.SET_NULL, null=True, blank=True)
purchase_order = models.ForeignKey('PurchaseOrder', on_delete=models.CASCADE)
would appreciate any insight