๐ Django DRF Post API with Nested Categories
A clean and minimal Django REST Framework project built to practice API development from scratch.
This version includes nested serializers for related models, allowing posts to show their associated category in JSON responses.
- Post model with title, content, timestamp, and category
- Category model to organize posts
- ModelSerializer automatically converts models to JSON
- Nested serializer: PostSerializer includes category data
- ModelViewSet + Router provides full CRUD operations:
- List all posts
- Retrieve a single post
- Create new posts
- Update posts (full & partial)
- Delete posts
- Optional search and ordering on posts
- Python 3
- Django 5
- Django REST Framework
- SQLite (default development database)
1๏ธโฃ Clone the repository:
git clone https://github.com/banumariwan/post_api_project.git
cd post_api_project
2๏ธโฃ Create and activate a virtual environment:
bash
Copy code
python -m venv env
# Linux/Mac
source env/bin/activate
# Windows
env\Scripts\activate
3๏ธโฃ Install dependencies:
bash
Copy code
pip install djangorestframework
pip install -r requirements.txt # if available
4๏ธโฃ Apply migrations:
bash
Copy code
python manage.py migrate
5๏ธโฃ Create superuser (optional, for admin):
bash
Copy code
python manage.py createsuperuser
6๏ธโฃ Run the server:
bash
Copy code
python manage.py runserver
๐ Project Structure
bash
Copy code
post_api_project/
โ
โโโ posts/
โ โโโ models.py
โ โโโ serializers.py # nested CategorySerializer
โ โโโ views.py # ModelViewSet
โ โโโ urls.py # Router for CRUD
โ โโโ admin.py
โ โโโ apps.py
โ
โโโ post_api_project/
โ โโโ settings.py
โ โโโ urls.py
โ
โโโ manage.py
โโโ README.md
๐ API Endpoints
Endpoint Method Description
/api/posts/ GET List all posts
/api/posts/<id>/ GET Retrieve a single post
/api/posts/ POST Create a new post
/api/posts/<id>/ PUT Full update of a post
/api/posts/<id>/ PATCH Partial update of a post
/api/posts/<id>/ DELETE Delete a post
Each post includes nested category data:
json
Copy code
{
"id": 1,
"title": "Hello",
"content": "My first post",
"created_at": "2025-11-16T03:00:00Z",
"category": {
"id": 1,
"name": "Tech"
}
}
โญ Learning Outcomes
DRF setup and configuration
Returning JSON with ModelSerializer
Full CRUD with ModelViewSet and Router
Nested serializers for related models
Search and ordering for better API usability
๐ฎ Future Improvements
Add authentication & permissions
Expand relationships (e.g., authors, comments)
Add Postman tests or Swagger documentation
Extend to a full blog API
โค๏ธ Author
Banu Mariwan
GitHub: banumariwan