본문 바로가기
BE/Python & Django REST API

[Django REST API] 9. Introduction to Viewsets

by 건빵거늬 2021. 12. 20.
Django REST framework는 API endpoints를 만드는데 필요한 두 개의 helper classes를 제공한다.
APIView와 ViewSet이 그것이다. 앞서서 APIView를 다뤘고 여기서는 ViewSet을 다룬다.
1. What is a Viewset?
2. Create a simple Viewset
3. Add create, retrieve, update, partial_update and destroy functions

1. What is a Viewset?

HTTP methods에 mapping되는 함수를 정의하는 대신에, common API object actions에 mapping 되는 몇개의 functions을 받아들인다. 

  • def list: Getting a list of objects
  • def create: Creating new objects
  • def retrieve: Getting a specific object
  • def update: Updating an object
  • def partial_update: Updating part of an object
  • def destroy: Deleting an object

<APIView와의 차이>

 

따라서 standard database operations을 사용하는 상황에 좋고, database back-end와 interface하는 api를 만드는 가장 빠른 방법이기도 하다. 그런 예시들은 다음과 같다.

  1. performing simple CRUD interface to database
  2. managing predefined objects
  3. little to no customization to viewsets provided by Django REST Framework
  4. working with standard database structures

2. Create a simple Viewset

3. Add create, retrieve, update, partial_update and destroy functions

댓글