Views

Right, we'd better write some views then. Open tutorial/quickstart/views.py and get typing.

  1. from django.contrib.auth.models import User, Group
  2. from rest_framework import viewsets
  3. from tutorial.quickstart.serializers import UserSerializer, GroupSerializer
  4. class UserViewSet(viewsets.ModelViewSet):
  5. """
  6. API endpoint that allows users to be viewed or edited.
  7. """
  8. queryset = User.objects.all().order_by('-date_joined')
  9. serializer_class = UserSerializer
  10. class GroupViewSet(viewsets.ModelViewSet):
  11. """
  12. API endpoint that allows groups to be viewed or edited.
  13. """
  14. queryset = Group.objects.all()
  15. serializer_class = GroupSerializer

Rather than write multiple views we're grouping together all the common behavior into classes called ViewSets.

We can easily break these down into individual views if we need to, but using viewsets keeps the view logic nicely organized as well as being very concise.