DEV Community

Silas Ochieng
Silas Ochieng

Posted on

Day 4 :Building Models and Templates for Inventory and Orders Apps in My Django Store Manager

Hey Dev Community! πŸ‘‹

Today, I made significant progress on my Django project β€” store_manager. Specifically, I focused on building and refining models and templates for two core applications: the Inventory App and the Orders App. Here's a breakdown of what I did, how I approached it, and a few snippets to give you a feel of the project.

Project Structure Overview

My store_manager project includes two main apps:

  1. inventory: Manages products, categories, and stock.
  2. orders: Handles customer orders and order items.

Project structure

And then at the templates i have this structure
I created simple yet clean templates to list, create, and manage products and orders.as shown below in this structure
Template structure

Models creation

Building Inventory Models for My Django Store Manager App

In this, I’ll walk you through the models I built for the inventory app in my Django project store_manager. These models handle suppliers, categories, products, stock movements, and inventory alerts β€” all built with scalability and real-world functionality in mind.


Supplier Model

Handles details about suppliers providing the products.

class Supplier(models.Model):
    name = models.CharField(max_length=100, unique=True)
    contact_person = models.CharField(max_length=100)
    email = models.EmailField()
    phone = models.CharField(max_length=20)
    address = models.TextField()
    website = models.URLField(blank=True)
    notes = models.TextField(blank=True)
    is_active = models.BooleanField(default=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
Enter fullscreen mode Exit fullscreen mode

Category Model

Supports nested categories and slugs for SEO-friendly URLs.

class Category(models.Model):
    name = models.CharField(max_length=50, unique=True)
    slug = models.SlugField(unique=True)
    description = models.TextField(blank=True)
    parent = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, related_name='children')
    image = models.ImageField(upload_to='category_images/', null=True, blank=True)
    is_active = models.BooleanField(default=True)
Enter fullscreen mode Exit fullscreen mode

Product Model

Central model for inventory tracking

class Product(models.Model):
    name = models.CharField(max_length=100)
    slug = models.SlugField(unique=True)
    description = models.TextField(blank=True)
    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, related_name='products')
    supplier = models.ForeignKey(Supplier, on_delete=models.SET_NULL, null=True, related_name='products')
    unit_price = models.DecimalField(max_digits=10, decimal_places=2)
    cost_price = models.DecimalField(max_digits=10, decimal_places=2)
    quantity = models.PositiveIntegerField(default=0)
    reorder_level = models.PositiveIntegerField(default=5)
    sku = models.CharField(max_length=50, unique=True)
    barcode = models.CharField(max_length=50, blank=True)
    is_active = models.BooleanField(default=True)
Enter fullscreen mode Exit fullscreen mode

Product image models

class ProductImage(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='images')
    image = models.ImageField(upload_to='product_images/')
    is_default = models.BooleanField(default=False)
Enter fullscreen mode Exit fullscreen mode

StockMovement Model

Logs every product movement: sales, purchases, losses, and more.

class StockMovement(models.Model):
    MOVEMENT_TYPES = [
        ('purchase', 'Purchase'),
        ('sale', 'Sale'),
        ('return', 'Return'),
        ('adjustment', 'Adjustment'),
        ('transfer', 'Transfer'),
        ('loss', 'Loss'),
    ]
    product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='movements')
    movement_type = models.CharField(max_length=20, choices=MOVEMENT_TYPES)
    quantity = models.IntegerField()
    before_quantity = models.IntegerField(editable=False)
    after_quantity = models.IntegerField(editable=False)
Enter fullscreen mode Exit fullscreen mode

Conclusion

This post covered the core models that power the Inventory side of my Django store_manager app β€” from suppliers and categories to products, stock movement, and inventory alerts. These models lay the foundation for tracking product availability, managing suppliers, and triggering automated alerts in real-time.

πŸ‘‰ In the next article, I’ll walk you through the Orders app models, where we’ll explore how orders are placed, managed, and linked to inventory changes. Stay tuned and feel free to drop any questions or suggestions in the comments!

django #python #backend #webdev

Top comments (2)

Collapse
 
dotallio profile image
Dotallio

Really strong set of models for inventory, especially logging stock movements in detail. How do you handle real-time edge cases where inventory gets out of sync?

Collapse
 
jared_okello profile image
jared okello

Great Idea Mr. Keep working on it🀜