Django also provide a more higher level of abstraction for the model and the views called as ViewSets. This abstractions allows developer to concentrate on the modelling and the state rather than url construction. It is very slightly different than the view creating view classes but with viewsets we have to write less code and it makes more sense as lot of the repetative code is abstracted and we can choose to overwrite if required.
Lets continue from where we left and create a new model known as User in models.py file and execute makemigrations and migrate command to create this new model.
Once the model is created run the command “python manage.py makemigrations” and then “python manage.py migrate“
Similar to previous tutorials, we create a standard serializer
Now lets create a new viewset for User model as below by extending the ModelViewSet Class.
remember with class based views, we had to create view classes for list views and the model view. But here we are creating only one viewset class.
Update the urls.py file as below. Notice that we just have to register the viewset to router and all the urls configuration will be abstracted for us and will be handled by the Router. So this has reduced our code of creating list view and also creating a different url patterns
Now lets test and see it in action. So this should serve our purpose for time being. We will look at different scenarios and ways to handle in the next tutorial