Skip to content

Django REST Framework API with nested serializers, ModelViewSet, and Router for full CRUD operations.

Notifications You must be signed in to change notification settings

banumariwan/post_api_project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

10 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“„ 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.


๐Ÿš€ Features

  • 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

๐Ÿ› ๏ธ Tech Stack

  • Python 3
  • Django 5
  • Django REST Framework
  • SQLite (default development database)

๐Ÿ“ฆ Installation

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

About

Django REST Framework API with nested serializers, ModelViewSet, and Router for full CRUD operations.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages