API-View vs View-Set in drf


In Django REST Framework (DRF), you can choose between `APIView` and `ViewSet` depending on your specific requirements and preferences.


`APIView` is a class-based view that provides methods corresponding to HTTP methods (e.g., `get()`, `post()`, `put()`, `delete()`, etc.). You can define these methods to handle the corresponding HTTP requests and implement your desired logic. It offers a great deal of flexibility and control over the API endpoints. You can use `APIView` when you need fine-grained control and customization over the API view logic.


On the other hand, `ViewSet` is a higher-level abstraction that combines multiple methods into a single class. It provides a set of predefined methods for common actions such as listing all objects, retrieving a single object, creating an object, updating an object, and deleting an object. `ViewSet` is more concise and can simplify the implementation of CRUD (Create, Retrieve, Update, Delete) operations for your API endpoints. It automatically maps these actions to appropriate HTTP methods. You can use `ViewSet` when you want a more concise and convention-based approach to defining your API views.


`ViewSet` also provides additional features such as support for pagination, filtering, and ordering out of the box. It is backed by a `ModelSerializer` for handling serialization and deserialization of model instances. `ViewSet` can be further customized by overriding specific methods or using mixins to add extra functionality.


In summary, if you prefer fine-grained control and customization, or if your API endpoints require complex logic, you can use `APIView`. If you want a more concise and convention-based approach, along with built-in support for common actions, you can use `ViewSet`.


date:Sept. 5, 2023