How to Create an ERP System for Car Manufacturing Using Django, Docker Compose, PostgreSQL, and DRF for API

Creating an ERP system for car manufacturing can enhance operations by centralizing processes such as inventory management, production scheduling, and quality control. In this guide, we will build an ERP system using Django, Docker Compose, PostgreSQL, and Django Rest Framework (DRF) for API integration. This setup will allow for an efficient, scalable, and easy-to-deploy solution for managing a car manufacturing business.

Why Use Django?

Django is the ideal framework for building ERP systems because of its numerous advantages:

  1. Rapid Development: With built-in features like an ORM, admin interface, and authentication system, Django allows developers to focus on business logic rather than boilerplate code.
  2. Scalability: Django scales easily, making it perfect for enterprise-grade applications like ERP systems, capable of handling increased data and user loads.
  3. Security: Django comes with security best practices by default, offering protections against common vulnerabilities.
  4. Modularity: Django’s modular structure promotes clean, maintainable code, enabling the efficient management of various ERP components.
  5. API Integration: With Django Rest Framework (DRF), Django easily exposes data and functionalities through APIs, which are essential for integrating with other systems or mobile apps.

Key Technologies

  • Django: A Python-based web framework used for building robust, scalable web applications.
  • Docker Compose: A tool to run multi-container Docker applications, making deployment seamless.
  • PostgreSQL: An open-source, powerful relational database system for managing complex data.
  • DRF: A toolkit for building APIs in Django, providing flexibility in handling HTTP-based interactions.

Step-by-Step Guide to Building the ERP System

1. Setting up the Project with Docker Compose

Start by creating the project directory and setting up the docker-compose.yml file to define the services for Django and PostgreSQL.

mkdir car_manufacturing_erp
cd car_manufacturing_erp

Create a docker-compose.yml file to manage the containers for Django and PostgreSQL:

version: '3'

services:
  db:
    image: postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      POSTGRES_DB: car_manufacturing_db
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: secretpassword
      POSTGRES_HOST_AUTH_METHOD: trust
    ports:
      - "5432:5432"

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/app
    ports:
      - "8000:8000"
    depends_on:
      - db
    environment:
      POSTGRES_DB: car_manufacturing_db
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: secretpassword
      POSTGRES_HOST: db
      POSTGRES_PORT: 5432

volumes:
  postgres_data:

Here, we define two services:

  • db: This service runs PostgreSQL. The database configuration is controlled via environment variables.
  • web: This service runs Django, with environment variables for PostgreSQL integration.

2. Creating the Django Project with Docker Compose

Use Docker Compose to create the Django project within the container:

docker compose run web django-admin startproject car_erp .

This command creates a Django project inside the container and maps it to your local file system.

Create an app for the core ERP functionalities, such as managing car models, parts, and production orders:

docker compose run web python manage.py startapp erp_core

3. Configuring PostgreSQL in Django Settings

Update the DATABASES configuration in settings.py to use the environment variables defined in the docker-compose.yml file:

import os

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.getenv('POSTGRES_DB'),
        'USER': os.getenv('POSTGRES_USER'),
        'PASSWORD': os.getenv('POSTGRES_PASSWORD'),
        'HOST': os.getenv('POSTGRES_HOST', 'db'),
        'PORT': os.getenv('POSTGRES_PORT', '5432'),
    }
}

This setup ensures that the database credentials are dynamically loaded from the Docker environment.

4. Defining Data Models for Car Manufacturing

Now, let’s define the models that represent the core entities of the car manufacturing ERP. These include car models, car parts, machines, production orders, and inventory.

In models.py:

from django.db import models

class CarModel(models.Model):
    name = models.CharField(max_length=255)
    year = models.IntegerField()
    production_line = models.CharField(max_length=255)

    def __str__(self):
        return f"{self.name} ({self.year})"

class CarPart(models.Model):
    part_name = models.CharField(max_length=255)
    part_number = models.CharField(max_length=100, unique=True)
    car_model = models.ForeignKey(CarModel, related_name='parts', on_delete=models.CASCADE)
    cost = models.DecimalField(max_digits=10, decimal_places=2)
    supplier = models.CharField(max_length=255)
    stock_quantity = models.IntegerField()

    def __str__(self):
        return f"{self.part_name} (Part No: {self.part_number})"

class Inventory(models.Model):
    part = models.ForeignKey(CarPart, on_delete=models.CASCADE)
    stock_quantity = models.IntegerField()
    unit_price = models.DecimalField(max_digits=10, decimal_places=2)

class Machine(models.Model):
    name = models.CharField(max_length=255)
    status = models.CharField(max_length=50)

class ProductionOrder(models.Model):
    order_number = models.CharField(max_length=255, unique=True)
    car_model = models.ForeignKey(CarModel, on_delete=models.CASCADE)
    machine = models.ForeignKey(Machine, on_delete=models.CASCADE)
    start_time = models.DateTimeField()
    end_time = models.DateTimeField(null=True, blank=True)
  • CarModel: Represents a car model, such as its name, year, and production line.
  • CarPart: Represents individual parts of each car model. Parts are related to car models and tracked for cost and supplier information.
  • Inventory: Tracks inventory levels for car parts.
  • Machine: Represents the machines used in the manufacturing process.
  • ProductionOrder: Tracks production orders, including the car model and machine used.

5. Building APIs with Django Rest Framework

Next, create serializers for these models in serializers.py to expose them via APIs:

from rest_framework import serializers
from .models import CarModel, CarPart, Inventory, Machine, ProductionOrder

class CarPartSerializer(serializers.ModelSerializer):
    class Meta:
        model = CarPart
        fields = '__all__'

class CarModelSerializer(serializers.ModelSerializer):
    parts = CarPartSerializer(many=True, read_only=True)

    class Meta:
        model = CarModel
        fields = ['id', 'name', 'year', 'production_line', 'parts']

class InventorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Inventory
        fields = '__all__'

class MachineSerializer(serializers.ModelSerializer):
    class Meta:
        model = Machine
        fields = '__all__'

class ProductionOrderSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProductionOrder
        fields = '__all__'

Create views in views.py for handling requests:

from rest_framework import generics
from .models import CarModel, CarPart, Inventory, Machine, ProductionOrder
from .serializers import CarModelSerializer, CarPartSerializer, InventorySerializer, MachineSerializer, ProductionOrderSerializer

class CarModelListCreateView(generics.ListCreateAPIView):
    queryset = CarModel.objects.all()
    serializer_class = CarModelSerializer

class CarPartListCreateView(generics.ListCreateAPIView):
    queryset = CarPart.objects.all()
    serializer_class = CarPartSerializer

class InventoryListCreateView(generics.ListCreateAPIView):
    queryset = Inventory.objects.all()
    serializer_class = InventorySerializer

class MachineListCreateView(generics.ListCreateAPIView):
    queryset = Machine.objects.all()
    serializer_class = MachineSerializer

class ProductionOrderListCreateView(generics.ListCreateAPIView):
    queryset = ProductionOrder.objects.all()
    serializer_class = ProductionOrderSerializer

6. Running the ERP System with Docker Compose

To start the system, first, build and run the Docker containers:

docker compose build
docker compose up

Apply the database migrations to set up the PostgreSQL schema:

docker compose run web python manage.py migrate

Your Django application will be available at http://localhost:8000, and the API will be accessible for managing car models, parts, inventory, and production orders.

Conclusion

By using Django, Docker Compose, PostgreSQL, and DRF, you've built a scalable and efficient ERP system tailored for car manufacturing. This system allows the management of car models, parts, inventory, and production orders, all of which are essential for the smooth operation of a car manufacturing plant. Additionally, with the power of Docker Compose, the deployment process becomes streamlined and easily portable across different environments.

Articles

Our Products


Articles

Our Products


Get in Touch with us

Speak to Us or Whatsapp(+66) 83001 0222

Chat with Us on LINEiiitum1984

Our HeadquartersChanthaburi, Thailand