1pip install djangorestframework
2pip install markdown # Markdown support for the browsable API.
3pip install django-filter # Filtering support
1def get_permissions(self):
2 """
3 Instantiates and returns the list of permissions that this view requires.
4 """
5 if self.action == 'list':
6 permission_classes = [IsAuthenticated]
7 else:
8 permission_classes = [IsAdmin]
9 return [permission() for permission in permission_classes]
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]