Home > Net >  How to create a subtraction in the inventory when a order is made? [Django] [Python] [E-commerce web
How to create a subtraction in the inventory when a order is made? [Django] [Python] [E-commerce web

Time:10-30

I’m a python/django begginer. I decided to build a e-commerce website using django for an academic project. I’ve been able to develop enough of the project and build understanding of the subject, but right now I’m having issues finding a way to subtracts the number of items listed in the inventory whenever a order is made.

That’s the code for the models, evey product has it's own stock quantity call inventory:

class Product(models.Model):
    name = models.CharField(max_length=200, null=True)
    price = models.FloatField()
    description = models.TextField(default='', null=True, blank=True)
    digital = models.BooleanField(default=False,null=True, blank=True)
    image = models.ImageField(null=True, blank=True)
    inventory = models.IntegerField(default=0)

    def __str__(self):
        return self.name

    def has_inventory(self):
        return self.inventory > 0

This is the code I made to subtract base on quantity of the item ordered, but I can’t make it work, it won’t subtract the number of items from the inventory on the product stock.

class OrderItem(models.Model):
    product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
    order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
    quantity = models.IntegerField(default=0, null=True, blank=True)
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return str(self.product)   " x "   str(self.quantity)

    def inventory(self):
        product.inventory = self.inventory
        product.inventory -= int(self.quantity)
        return inventory

What could I do to make it work?

CodePudding user response:

All logic/action should be written under views.py file. You could create a function where it takes in a request, and when it does, it takes in all the value inputted through a form, and you could use filter to filter out the products you want to subtract its inventory and update query by Django to update the inventory.

It should look something like this inside your views function:

Product.objects.filter(name = name, description = description, digital = digital).update(Inventory = F('Inventory')-inventory)

Here is Django's documentation on queries: Django's Making Queries

CodePudding user response:

I think there are a few problems with the snippet above.

First, the OrderItem.inventory is not referring the right value, it should be like the snippet below.

    def inventory(self):
        // remember the current stock is stored on Product.inventory
        return self.product.inventory - self.quantity

Second, The method name should be remaining_stock not inventory to prevent misunderstanding.

    def remaining_stock(self):
        return self.product.inventory - self.quantity

Also, don't forget if you want to store inventory of the product please call the save method after successfully inserting the OrderItem.

  • Related