[Django]-How can I choose the type of view in Django Rest Framework

5👍

There is no standard but you can start with this simple strategy:

  1. Specific action on a model class — generic views (RetrieveAPIView,
    ListAPIView, UpdateAPIView, etc.)
  2. Several actions in one class and basic CRUD — ViewSets (ModelViewSet and ReadOnlyModelViewSet are most useful)
  3. Some action on 1 instance — ViewSet + @action(detail=True)
  4. Some action on several or all objects — ViewSet +@action(detail=False)
  5. Simplest custom actions — function based views or @action again.

Also check DRF views classes for quick overview.

Leave a comment