When to use views, generic views and ViewSets in django rest framework?

Md Sadiqul Islam
2 min readMar 5, 2023

--

In Django REST framework, views, generic views, and ViewSets are used to define how requests to the API are handled and what response should be returned.

  1. Views: Views are the basic building blocks of any Django application. In Django REST framework, views are used to handle requests and generate responses. You can define your own views by creating a subclass of the Django View class, or by using one of the built-in views provided by the framework.
  2. Generic Views: Generic views are pre-built views that can be used for common use cases like creating, updating, and deleting objects. They are built on top of the regular views and provide a lot of functionality out of the box. Examples of generic views include CreateAPIView, ListAPIView, RetrieveAPIView, and UpdateAPIView.
  3. ViewSets: ViewSets are another way of defining views in Django REST framework. They are classes that group related views together and provide a set of default actions for CRUD operations (Create, Retrieve, Update, Delete) that can be customized as needed. ViewSets are especially useful when working with complex APIs that have multiple endpoints and require a lot of code to handle.

When to use each one depends on the complexity and requirements of your API. If your API is simple and only requires a few endpoints, you can use regular views. If your API requires more functionality, you can use generic views to reduce the amount of boilerplate code. Finally, if your API is very complex, you can use ViewSets to organize your code and provide a consistent interface.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response