Ali Rn
Ali Rn

Reputation: 1214

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

I can create my view in Django with :

Now is there any standard to tell us when should we use which?

and why we have this much type at all?

Upvotes: 4

Views: 659

Answers (1)

Mark Mishyn
Mark Mishyn

Reputation: 4151

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.

Upvotes: 6

Related Questions