MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Build REST API for E-commerce App0%
Study Materials
REST API Development with DjangoProgress 0%
1.1REST Principles1.2HTTP Methods Overview1.3Status Codes
10.1Build REST API for E-commerce App10.2CRUD, Authentication, Filtering, Pagination10.3Test & Document API
HomeTutorialsREST API Development with DjangoBuild REST API for E-commerce App
CRUD, Authentication, Filtering, Pagination

Build REST API for E-commerce App

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

DRF Capstone: E-Commerce API Data Model and Architecture

In this capstone series, we design and build a production-grade, multi-model E-Commerce REST API. We begin by architecting the relational database schema, model relationships, managers, and data flow.


1. System Architecture & Entity Relationships

An enterprise E-Commerce API requires strict relational integrity across products, categories, shopping carts, orders, and user profiles:

1[ User ] (Django Auth)
2 │
3 ├── (1:1) ── [ CustomerProfile ] (phone, address)
4 │
5 ├── (1:N) ── [ Order ] (status, created_at, total)
6 │ │
7 │ └── (1:N) ── [ OrderItem ] (product, unit_price, quantity)
8 │ │
9 └── (1:1) ── [ Cart ] │
10 │ ▼
11 └── (1:N) ── [ CartItem ] ── (N:1) ── [ Product ] ── (N:1) ── [ Category ]

2. Model Definitions: Products & Categories

In store/models.py:

1import uuid
2from django.db import models
3from django.conf import settings
4from django.core.validators import MinValueValidator
5 
6class Category(models.Model):
7 name = models.CharField(max_length=100, unique=True)
8 slug = models.SlugField(max_length=120, unique=True)
9 description = models.TextField(blank=True)
10 
11 class Meta:
12 verbose_name_plural = 'Categories'
13 ordering = ['name']
14 
15 def __str__(self):
16 return self.name
17 
18class Product(models.Model):
19 category = models.ForeignKey(Category, on_delete=models.PROTECT, related_name='products')
20 title = models.CharField(max_length=200)
21 slug = models.SlugField(max_length=220, unique=True)
22 description = models.TextField()
23 price = models.DecimalField(max_digits=10, decimal_places=2, validators=[MinValueValidator(0.01)])
24 inventory = models.PositiveIntegerField()
25 is_active = models.BooleanField(default=True)
26 created_at = models.DateTimeField(auto_now_add=True)
27 updated_at = models.DateTimeField(auto_now=True)
28 
29 class Meta:
30 ordering = ['-created_at']
31 
32 def __str__(self):
33 return self.title

3. Carts, Orders, and Items

1class Cart(models.Model):
2 id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
3 created_at = models.DateTimeField(auto_now_add=True)
4 
5 def __str__(self):
6 return str(self.id)
7 
8class CartItem(models.Model):
9 cart = models.ForeignKey(Cart, on_delete=models.CASCADE, related_name='items')
10 product = models.ForeignKey(Product, on_delete=models.CASCADE)
11 quantity = models.PositiveSmallIntegerField(validators=[MinValueValidator(1)])
12 
13 class Meta:
14 unique_together = [['cart', 'product']]
15 
16class Order(models.Model):
17 STATUS_PENDING = 'P'
18 STATUS_COMPLETE = 'C'
19 STATUS_FAILED = 'F'
20 STATUS_CHOICES = [
21 (STATUS_PENDING, 'Pending'),
22 (STATUS_COMPLETE, 'Complete'),
23 (STATUS_FAILED, 'Failed'),
24 ]
25 
26 id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
27 customer = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, related_name='orders')
28 placed_at = models.DateTimeField(auto_now_add=True)
29 payment_status = models.CharField(max_length=1, choices=STATUS_CHOICES, default=STATUS_PENDING)
30 shipping_address = models.CharField(max_length=255)
31 
32 class Meta:
33 ordering = ['-placed_at']
34 
35class OrderItem(models.Model):
36 order = models.ForeignKey(Order, on_delete=models.PROTECT, related_name='items')
37 product = models.ForeignKey(Product, on_delete=models.PROTECT)
38 unit_price = models.DecimalField(max_digits=10, decimal_places=2)
39 quantity = models.PositiveSmallIntegerField()

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Why is on_delete=models.PROTECT used for category on the Product model instead of CASCADE?
Question 2
Why is a UUID primary key preferable to an auto-incrementing integer ID for Cart and Order models?
Question 3
Why does OrderItem store a separate unit_price field instead of referencing product.price dynamically?
Question 4
What constraint does unique_together = [['cart', 'product']] enforce on CartItem?
Question 5
What does MinValueValidator(1) enforce on CartItem.quantity?
Next Lesson

CRUD, Authentication, Filtering, Pagination

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Documenting API EndpointsCRUD, Authentication, Filtering, Pagination

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master REST API Development with Django with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext