July 2018
Intermediate to advanced
324 pages
8h 14m
English
Django Rest Framework has a module named viewsets. It follows a similar pattern as the Django default views functionality but extends features to work with the serializer class. Read more about the viewsets at http://www.django-rest-framework.org/api-guide/viewsets/. The following is the code snippet for viewsets classes related to Photo and PhotoAlbum models.
File—gallery/views.py:
from rest_framework import viewsetsfrom gallery.models import Photo, PhotoAlbumfrom gallery.serializers import PhotoSerializer, PhotoAlbumSerializerclass PhotoViewset(viewsets.ModelViewSet): queryset = Photo.objects.all() serializer_class = PhotoSerializer def get_queryset(self, *args, **kwargs): if 'album_id' not in self.kwargs: raise APIException('required ...Read now
Unlock full access