Learn how to build a powerful and flexible CRUD (Create, Read, Update, Delete) REST API using the latest Django 5.2.3, Python 3.13.3, and PostgreSQL. This guide is ideal for backend developers seeking to utilize Django REST Framework (DRF) for developing scalable APIs.
Prerequisites
To follow along, make sure you have the following installed:
-
Python 3.13.3 — the latest Python version with performance improvements
-
PostgreSQL — a powerful open-source relational database
-
Basic knowledge of Django
-
pip
orpipenv
to manage Python packages
1. Install Python 3.13.3 (if not already installed)
Ensure Python 3.13.3 is installed. This is essential to create a modern Django environment.
Download from: https://www.python.org/downloads/release/python-3133/
Verify your Python version:
python3 --version
# Output should be Python 3.13.3
2. Create a Django Project
Set up a virtual environment and install Django along with the necessary packages. This isolates dependencies from your global Python installation.
python3 -m venv env
source env/bin/activate # Use `env\Scripts\activate` on Windows
pip install django==5.2.3 djangorestframework psycopg2-binary
django-admin startproject drf_postgres_crud
cd drf_postgres_crud
3. Configure PostgreSQL Database
Connect your Django project to a PostgreSQL database. Update the database settings in settings.py
to match your PostgreSQL credentials.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'drf_crud_db',
'USER': 'your_db_user',
'PASSWORD': 'your_db_password',
'HOST': 'localhost',
'PORT': '5432',
}
}
Create the database from your PostgreSQL client:
psql postgres -U djamware
CREATE DATABASE drf_crud_db;
\q
4. Create a Django App
Django is modular, and apps are components that make up a project. Create one for your API logic.
python3 manage.py startapp api
Add the new app and DRF to your project’s installed apps in settings.py
:
INSTALLED_APPS = [
...
'rest_framework',
'api',
]
>5. Create a Model
Models define the structure of your database tables. Here, we define a Article
model with common fields.
In api/models.py
:
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
published = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
Apply the model changes to the database:
python3 manage.py makemigrations
python3 manage.py migrate
6. Create a Serializer
Serializers convert complex data like model instances to JSON. DRF uses them to validate and render input/output.
In api/serializers.py
:
from rest_framework import serializers
from .models import Article
class ArticleSerializer(serializers.ModelSerializer):
class Meta:
model = Article
fields = '__all__'
7. Create a ViewSet
ViewSets in DRF abstract common logic for CRUD operations. This reduces boilerplate code.
In api/views.py
:
from rest_framework import viewsets
from .models import Article
from .serializers import ArticleSerializer
class ArticleViewSet(viewsets.ModelViewSet):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
8. Create API URLs
Use DRF’s DefaultRouter
to automatically generate URL patterns for your API endpoints.
In api/urls.py
:
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import ArticleViewSet
router = DefaultRouter()
router.register(r'articles', ArticleViewSet)
urlpatterns = [
path('', include(router.urls)),
]
Include these API routes in your main project URLs.
In drf_postgres_crud/urls.py
:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('api.urls')),
]
9. Run the Server and Test
Start the Django development server:
python3 manage.py runserver
Open http://localhost:8000/api/articles/ in your browser. You should see DRF’s interactive UI.
Available endpoints:
-
GET /api/articles/
— List all articles -
POST /api/articles/
— Create a new article -
GET /api/articles/:id/
— Retrieve a specific article -
PUT /api/articles/:id/
— Update a specific article -
DELETE /api/articles/:id/
— Delete a specific article
10. Enable CORS (Optional for Frontend Access)
If you're building a frontend in React, Angular, or Vue, enable CORS to allow cross-origin API access.
Install the CORS headers package:
pip install django-cors-headers
Update settings:
INSTALLED_APPS += ['corsheaders']
MIDDLEWARE = ['corsheaders.middleware.CorsMiddleware', ...]
CORS_ALLOW_ALL_ORIGINS = True # Set to specific origins in production
11. Conclusion
You’ve successfully built a modern CRUD REST API using Django 5.2.3, DRF, and PostgreSQL. This backend is fully capable of serving data to modern frontend frameworks or mobile apps.
Next Steps:
-
Add authentication using JWT or session-based auth
-
Add filtering, pagination, and search capabilities
-
Deploy the app using platforms like Heroku, Railway, or Docker
You can get the full source code from our GitHub.
That's just the basics. If you need more deep learning about Python and the frameworks, you can take the following cheap course:
- Edureka's Django course helps you gain expertise in Django REST framework, Django Models, Django AJAX, Django jQuery etc. You'll master Django web framework while working on real-time use cases and receive Django certification at the end of the course.
- Unlock your coding potential with Python Certification Training. Avail Flat 25% OFF, coupon code: TECHIE25
- Database Programming with Python
- Python Programming: Build a Recommendation Engine in Django
- Python Course:Learn Python By building Games in Python.
- Learn API development with Fast API + MySQL in Python
- Learn Flask, A web Development Framework of Python
Thanks!