Creating Models for API

What is a Model in Django?

  A Model in Django is like a blueprint for your database table.
  It tells Django:
     What fields you want to store (like title, author, price, etc.)
     What type of data each field will hold (like text, number, date).

Think of it like creating a form structure — where you define what information you’ll collect and store.

Where Do We Create Models in Django Project?

When you create a Django project, you also create apps inside it.
Example folder structure:
myproject/

├── books/ ← Your Django App
│ ├── models.py ← This is where you define models
│ ├── views.py
│ ├── urls.py
│ └── …
└── manage.py

  Inside your app folder (books in this case), there is a file called models.py.
  You will write all your database table definitions here using Python code.

Creating and defining a Book Model

Let’s now create our first model called Book inside the books/models.py file.

from django.db import models # importing Django’s models module
# Create your models here.
# books/models.py
class Book(models.Model):
title = models.CharField(max_length=100) # Book title (text field, max 100 characters)
author = models.CharField(max_length=100) # Author name (text field, max 100 characters)
price = models.DecimalField(max_digits=6, decimal_places=2) # Decimal price (like 199.99)
published_date = models.DateField() # Date field (e.g., 2024-03-12)
def __str__(self):
return self.title # When we print the Book object, it will show the title

Explanation of Each Line of code:

from django.db import models
// This line imports the models module from Django.
It contains built-in classes that help you create database tables using Python code. You’ll use these to define fields like text, number, or date in your model.

class Book(models.Model):
// This defines a new class named Book which represents a database table.
By writing Book(models.Model), you’re telling Django to treat this class as a model and create a table for it in the database.

title = models.CharField(max_length=100)
// This creates a field named title in the Book table that stores short text.
CharField is used to store strings (text). max_length=100 means the title can be up to 100 characters long.

author = models.CharField(max_length=100)
// This creates another text field called author to store the name of the book’s author.
Again, CharField is used, with a maximum length of 100 characters.

price = models.DecimalField(max_digits=6, decimal_places=2)
// This creates a price field for the book that stores decimal numbers.
DecimalField is ideal for prices or money. max_digits=6 means the total number of digits can be up to 6 (like 9999.99), and decimal_places=2 means 2 digits after the decimal point.

published_date = models.DateField()
// This creates a field called published_date to store the date the book was published.
DateField is used to save date values in the format YYYY-MM-DD (e.g., 2023-05-10).

def __str__(self):
// This is a special method that tells Django what to display when you view a Book object.
This is helpful when you are checking objects in the admin panel or debugging.


return self.title
// This line makes the model return the book’s title when printed.
So instead of seeing something like <Book object (1)>, you’ll see the actual title of the book.

Step: Activating Models - Making Migrations

Once you define the model, you need to tell Django to create these tables in the database.

Step 1: Make Migrations

Open terminal in your project folder and run:

python manage.py makemigrations

This will create a migration file. It’s like a plan that tells Django what database changes are needed.

Example output:
Django-make-mgr

Step 2: Apply Migrations to Database

Now apply these changes to your actual database:

python manage.py migrate

This will create the Book table in your database behind the scenes.

Example output:
Django-apply-mgration

Exercise:

  • Create a model called Student with fields: name, age, grade, and email.

  • Run migrations and check if the table appears in the database.

Course Video in English