Forums

Problem update model instance to database

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

was able to make it work doing it like this

@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():
    for inv_line in inventory_line:
        inv_line.quantity += instance.quantity
        inv_line.save()
else:
    Inventory.objects.create(
        quantity=instance.quantity,
        condition=instance.product_condition,
        product=instance.product
    )

This is a better but I still don't know why I can't update an object of the queryset when referencing it via object[0].save()

That is odd. I can't see any particular reason why that would fail -- it would be interesting to know what the value of inventory_line[0].id was in that case. That said, I guess if you have something that's working now then perhaps you won't have the time to check that out.

When I was messing with it trying to get it to work I tried

def save_inventory_line(sender, instance, **kwargs):
inventory_line = Inventory.objects.filter(product=instance.product, condition=instance.product_condition)
if inventory_line.exists():
    inv = Inventory.objects.get(pk=inventory_line[0].id)
    inv.quantity += instance.quantity
    inv.save()
    ...
    ...

And that worked just fine. When it did fail with inventory[0].save() I didn't get any errors it just didn't update the inventory table

How odd! There's clearly something I don't understand about the objects in Django querysets. If you really want to get to the bottom of it, perhaps a post to Stack Overflow will provide an answer?