From the course: Building Angular and Django Apps
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Creating a ViewSet with Django
From the course: Building Angular and Django Apps
Creating a ViewSet with Django
- Django REST Framework provides an easy way to grade common operations for models and objects in a REST API. Typically, for any object exposed by the API, we will want to be able to create, update, delete, retrieve them, or retrieve a list of them. ViewSets provide that functionality. It's very straightforward to create, and we will be replacing our existing PackageCreateView with a new viewset. First we import viewsets from Django REST Framework, and then we replace PackageCreateView with PackageViewSet, which is a subclass. The queryset remains the same. It points to all packages. And the serializer class is the PackageSerializer. Now in the URLs configuration, we have to remove the PackageCreateView, and we're going to be altering the URL so that multiple models can be exposed by this API. We're going to include URLs from the router. And we're going to register the PackageViewSet. Then we can run the Django server.…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.