This is Part 3 of the series "Learn Backend Development by Building a Social Media App.
Welcome to Part 3.
So far you've understood what a backend is, created your first Django project, and seen your server run for the very first time. Now we move into one of the most important ideas in backend development: data.
Everything in a social media app revolves around data. Users are data. Posts are data. Comments, likes, stories, notification, all data. You're going to learn how that data is stored, how Django interacts with it, and how we begin shaping something as fundamental as a user account.
Today's chapter is long because this is the foundation of everything else. We're going to go step by step in a slow, friendly manner, and by the end, you'll truly understand what happens inside the backend whenever someone creates an account.
Before We Code: What Exactly Is a Database?
A database is simply a place where your backend stores information. Think of it like a very organized notebook. Every time someone registers, a fresh entry is added to the notebook. Every time someone posts a picture, the notebook receives another entry. Every like, every comment, every follow, everything goes into this book.
The difference between a real notebook and a database is that the database can handle millions of entries without getting tired.
Your app might have thousands of users and thousands of posts. A database is where all of that lives, safely and permanently.
Django communicates with the database for you. You don't write SQL. Django does the hard work.
Understanding Models in Django
If a database is like a notebook full of organized information, then a model is the template for how a particular kind of information should look.
For example, if you're designing a user, you need to tell your backend what information a user should contain. Maybe a username, an email, a password, a profile picture, a date they joined, and so on. A model is how you describe that structure.
A simple way to think of it:
Model = Blueprint
Database Table = Building made using that blueprint
Database Row = One specific user built using that blueprint
You write the blueprint once in your Django code. Django creates the building (the table). Every time someone registers, Django adds a row to that table.
Django's Built-In User Model (And Why We Don't Use It)
Django comes with a built-in User model. It already includes username, password, email, and several other properties.
But modern apps need more control. Social media especially needs:
- email login
- custom fields
- profile extension
- OTP verification
- unique behaviors
Because of this, many real projects don't use Django's default User. They create a custom user model.
Creating it early is important because changing it later becomes very complicated. So in this part, we'll build a custom user model from the ground up.
Creating Your First Django App
Inside your project folder, run:
python manage.py startapp accounts
This creates a new app called accounts. This app will contain:
- the custom user model
- registration logic
- login logic
- verification features later
Your folder structure now looks like this:
core/
accounts/
manage.py
The accounts folder will soon become the home of your entire authentication system.
Designing the Custom User Model
Let's open accounts/models.py. Delete everything inside it and write this:
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
email = models.EmailField(unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
def __str__(self):
return self.email
Now let's understand what just happend, slowly.
We're telling Django that:
- Our User is based on Django's built-in AbstractUser (so we still inherit many useful features like password handling).
- The default username is replaced with an email.
- Emails must be unique.
- When Django prints a user, it should show the email, not some random ID.
This gives us full control. Later parts of the series will add more fields like verification status, OTP codes, timestamps, and more.
Adding the Accounts App to Installed Apps (Important Step)
You just created a new Django app called accounts, but Django doesn't automatically know about it. You must tell Django to load it.
Open core/settings.py and find INSTALLED_APPS. Add "accounts" there like this:
INSTALLED APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts', # <-- Add here
]
Without this step, Django won't pick up your custom models at all, which would cause errors later.
Connecting the Custom User Model to Django
Django won't magically use you new user model unless you tell it to. So open core/settings.py and add this line at the bottom:
AUTH_USER_MODEL = 'accounts.User'
This line is extremely important. It says, "Hey Django, use this new User model instead of the old one".
Understanding Migrations (Very Important Concept)
You created a model. But right now, the database knows nothing about it.
A migration is like giving the database instructions about your blueprint. It's Django telling the database, "Please create a table for this model".
We run two commands:
python manage.py makemigrations
python manage.py migrate
The first command reads your model and prepares instructions. The second command applies those instructions to the actual database.
After running these commands, your User table now exists inside your database.
You haven't created any users yet, but the table is ready to hold them. Here's a simple diagram of what happened:
This is a process you'll repeat throughout this entire series.
Creating Your First User (Using the Admin System)
Django has a built-in admin panel that lets you interact with your data. But before we can us it, we need to create an admin user.
Run:
python manage.py createsuperuser
It will ask for an email , a username, and a password. Once done, start your server:
python manage.py runserver
Then visit:
http://127.0.0.1:8000/admin
You will see a login page. Enter your credentials, and you'll step into Django's administrator dashboard.
This dashboard will eventually show your posts, comments, users, stories, everything. Right now, it's empty, but that's perfect. It means we're ready to start filling it by building the actual registration and login system.
Where We Are Now
At this moment, your backend has:
- a working Django server
- a custom user model
- a connected database
- Django admin access
- your first superuser account
You've taken the first real step towards building your social media backend.
What's Coming in Part 4
In the next part, we begin turning this data into something users can actually interact with. You'll learn:
- what JWT is
- how login really works
- how sessions differ from token authentication
- how to implement registration using real API endpoints
- how to implement login
- how to secure protected routes
This is where your backend starts becoming a real API.
Till then, Happy Coding!!


Top comments (0)