Table of Contents
When I first started learning Django, I remember feeling overwhelmed by the sheer number of concepts—models, views, templates—it was like trying to drink from a firehose. But as soon as I grasped how models work, the rest of the framework started falling into place. Models are the foundation of any Django project, and in this tutorial, I want to walk you through building them, starting from the ground up. We'll cover everything, from setting up your environment to creating a functional Product
model. By the end, you’ll have a solid understanding of how models work and how to use them effectively.
Let’s not just dive in—we’ll take it step by step, so no detail is left unexplained.
Setting Up Django: From Scratch
The first step in any Django project is making sure your environment is properly set up. Before we even think about models, we need to prepare our workspace. On Ubuntu, this starts with ensuring you have Python installed, along with its package manager, pip. You can install both with a simple command:
sudo apt update && sudo apt install python3 python3-pip
Once that’s done, confirm the installation by running python3 --version and pip3 --version, which should display the installed versions of Python and pip. These tools are essential for everything we’ll be doing, so it’s worth double-checking they’re working.
Whenever I start a new Django project, I always set up a virtual environment first. This is a dedicated environment for your project’s dependencies, keeping them isolated from your system-wide Python installation. Think of it like a protective bubble for your project. To create one, you’ll need the venv
module, which is installed with Python by default. Navigate to your project folder and create a virtual environment:
python3 -m venv env
Activate it by running:
source env/bin/activate
You’ll notice your terminal prompt changes to include (env)
, letting you know the virtual environment is active.
With our environment set up, we’re ready to install Django itself. Using pip, install it with:
pip install django
You can check the version to ensure it installed correctly:
python -m django --version
This ensures everything is up to date and ready to go. Now comes the exciting part—starting a new Django project. For this tutorial, we’re creating a project called inventory_system
. Run:
django-admin startproject inventory_system .
The dot at the end is critical—it tells Django to create the project in the current directory instead of a nested folder. This keeps things neat and manageable.
If you take a look, Django will have created several files and folders for you. There’s the manage.py
file, which you’ll use to interact with the project, and a subfolder named after your project that contains configuration files like settings.py
and urls.py
. At this point, you can already run the development server:
python manage.py runserver
Open a browser, go to http://127.0.0.1:8000, and you’ll see the Django welcome page. It’s always a satisfying moment because it means everything is working as expected.
Creating a Django App for Products
A Django app is a modular piece of your project, usually representing a specific feature. For this tutorial, we’ll create an app to handle products and reviews. To generate the app’s structure, run:
python manage.py startapp products
Once the app is created, register it in your project. Open settings.py
and add 'products',
to the INSTALLED_APPS
list:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'products', # New app
]
This step tells Django to include the app in the project. With our app in place, we’re ready to start building models.
Introducing the Product Model
Models in Django are Python classes that define the structure of your database tables. Each attribute in the model becomes a column, and Django handles all the heavy lifting of converting these Python classes into actual SQL statements. For our example, we’ll start with a Product
model. This model will have fields for common attributes like the product name, description, price, and stock. Additionally, we’ll include a slug
field to create SEO-friendly URLs, a BooleanField
to mark whether the product is active, and timestamps for when the product was created and updated.
Here’s the code for the Product model:
from django.db import models
from django.utils.text import slugify
from django.urls import reverse
class Product(models.Model):
name = models.CharField(max_length=200)
description = models.TextField()
price = models.DecimalField(max_digits=10, decimal_places=2)
stock = models.IntegerField()
is_active = models.BooleanField(default=True)
slug = models.SlugField(unique=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
release_date = models.DateField(null=True, blank=True)
image = models.ImageField(upload_to='product_images/', null=True, blank=True)
def __str__(self):
return self.name
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.name)
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse('product_detail', kwargs={'slug': self.slug})
Breaking It Down
The Product
model uses several field types to represent different attributes of a product. For instance, the name
field is a CharField
with a maximum length of 200 characters, while the description
field is a TextField
for longer text. The price
field is a DecimalField
, which allows us to store precise monetary values by specifying the total number of digits (max_digits=10
) and the number of decimal places (decimal_places=2
). Similarly, stock
is an IntegerField
to represent inventory counts, and is_active
is a BooleanField
to indicate if the product is currently available.
The slug
field is particularly interesting. It’s a SlugField
, which is used to store URL-friendly text, often derived from the product name. To ensure every product has a unique slug, we set unique=True
. By overriding the save
method, we use Django’s slugify
function to automatically generate a slug when the product is saved for the first time.
For tracking timestamps, we’ve added created_at
and updated_at
fields. The created_at
field uses auto_now_add=True
to set the timestamp when the record is first created, while updated_at
uses auto_now=True
to update the timestamp whenever the record is modified. There’s also an image
field for storing product images. This requires the Pillow library, which you can install with pip install pillow.
Two additional methods enhance the usability of this model. The __str__
method defines how the product will appear in the Django admin or when printed. In this case, it simply returns the product’s name. The get_absolute_url
method generates a URL for the product, using its slug. This keeps URL logic centralized and avoids hardcoding paths elsewhere in the code.
Making Migrations
To apply this model to the database, we need to create and run migrations. This is how Django translates our Python code into database schema changes. First, create the migrations with:
python manage.py makemigrations
Then, apply them to the database with:
python manage.py migrate
If everything worked, Django will have created a products_product
table in your database. Your Product
model is now live and ready to use!
Wrapping Up Part 1
So far, we’ve set up Django, created a project and app, and built a functional Product
model. In the next part, we’ll create a Review
model, establish relationships between models, and dive into querying the database with Django’s ORM. If you’ve been following along, take some time to explore your new model—run the development server, experiment with the Django admin, and see it in action. Stay tuned for Part 2!