1pip install djangorestframework
2pip install markdown # Markdown support for the browsable API.
3pip install django-filter # Filtering support
1from django.contrib.auth.models import User, Group
2from rest_framework import viewsets
3from rest_framework import permissions
4from tutorial.quickstart.serializers import UserSerializer, GroupSerializer
5
6
7class UserViewSet(viewsets.ModelViewSet):
8 """
9 API endpoint that allows users to be viewed or edited.
10 """
11 queryset = User.objects.all().order_by('-date_joined')
12 serializer_class = UserSerializer
13 permission_classes = [permissions.IsAuthenticated]
14
15
16class GroupViewSet(viewsets.ModelViewSet):
17 """
18 API endpoint that allows groups to be viewed or edited.
19 """
20 queryset = Group.objects.all()
21 serializer_class = GroupSerializer
22 permission_classes = [permissions.IsAuthenticated]
1from django.conf.urls import url, include
2from django.contrib.auth.models import User
3from rest_framework import routers, serializers, viewsets
4
5# Serializers define the API representation.
6class UserSerializer(serializers.HyperlinkedModelSerializer):
7 class Meta:
8 model = User
9 fields = ['url', 'username', 'email', 'is_staff']
10
11# ViewSets define the view behavior.
12class UserViewSet(viewsets.ModelViewSet):
13 queryset = User.objects.all()
14 serializer_class = UserSerializer
15
16# Routers provide an easy way of automatically determining the URL conf.
17router = routers.DefaultRouter()
18router.register(r'users', UserViewSet)
19
20# Wire up our API using automatic URL routing.
21# Additionally, we include login URLs for the browsable API.
22urlpatterns = [
23 url(r'^', include(router.urls)),
24 url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
25]