Using Django for Inventory Management with FIFO (First In First Out) method

Steve Lukis
3 min readOct 7, 2021

--

A few months ago, I got a Point of Sale project which requires inventory management using FIFO (First In First Out method). I will share how I solve it with Python and Django as my tech stacks. It is expected for the readers to have some experience in Django. If you are working on a similar project, I hope this will be useful.

Understanding the FIFO system

In the inventory management system, there is a need to associate costs to goods. The thing is that the costs are not always static. They can change. There are a lot of factors, e.g. market inflation, seasons, pandemic, politics, etc. One way to associate cost to goods is the FIFO method.

The FIFO method assign the oldest costs to goods sold. Then we reduce the number of stock for oldest cost from the inventory. This is the most widely used method of valuing inventory, because it actually makes sense. We expect to sell the first goods we bought. This offers a better picture of the actual flow of goods and the costs.

Implementation in Django

Note: I cut all the unnecessary details to make my point clearer

I have a Product model like this:

class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=15,
decimal_places=0,
default=0)

We can’t store the number of stock and the cost in the Product model because it always change. So we need to create a Stock model.

class Stock(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
timestamp = models.DateTimeField(null=True,
blank=True)
cost = models.DecimalField(null=True,
blank=True,
max_digits=15,
decimal_places=0)
quantity = models.IntegerField(null=False, default=0)

Look that we have a timestamp field so we can determine the oldest goods.

I created this function that serve the processing of stocks with the FIFO methods every time goods are sold:

def process_stocks_fifo(product, quantity):
"""
product: The product that will be sold
quantity: The number of items of goods that will be sold
"""
qty = quantity
stocks = Product.stock_set.all().order_by('timestamp')
# Looping stocks from the oldest one
for stock in stocks:
if qty > 0:
# The quantity left for this stock row
qty_left = qty
# Subtract qty from the stock quantity
qty -= stock.quantity
if qty >= 0:
# If qty >= 0, that means stock.quantity
# is not sufficient, the calculation will
# be continued on the next stock row and
# the current stock will be deleted as it
# has been empty
stock.delete()
else:
# If qty < 0, that means the current stock
# row is enough and it will be updated
stock.quantity = stock.quantity - qty_left
stock.save()
else:
break

Essentially what we are doing up there are reducing the oldest stocks every time there are goods sold. If the oldest stock is sold out, then the next oldest stock will be reduced for the remaining quantity. This keeps looping until the quantity requested is fulfilled.

That’s it for the FIFO method in inventory management for POS app. I use this in Django. But the algorithm should be useful in other tech stacks. I hope you find it useful.

--

--