Django REST Framework Tutorial: Build APIs with ViewSet

preview_player
Показать описание
Django REST Framework ViewSet Made Easy (Beginner Friendly)

Back-end Python development

Class based view api

In Django REST Framework (DRF), a ViewSet is a class-based view that provides actions like list, retrieve, create, update, and destroy without having to explicitly define each method.

1. Create a Model
Define the database structure using Django models.

2. Create a Serializer
Convert the model instances into JSON format and validate incoming data.

3. Create a ViewSet
Use DRF's ModelViewSet to handle common CRUD operations.

4. Register ViewSet with Router
DRF routers automatically create URLs for ViewSet actions.

ViewSet Actions:

GET /books/ → List all books

GET /books/1/ → Retrieve book with ID 1

POST /books/ → Create a new book

PUT /books/1/ → Update book with ID 1

DELETE /books/1/ → Delete book with ID 1
Рекомендации по теме
welcome to shbcf.ru