1from django.contrib.auth.models import User, Group
2from rest_framework import serializers
3
4
5class UserSerializer(serializers.HyperlinkedModelSerializer):
6 class Meta:
7 model = User
8 fields = ['url', 'username', 'email', 'groups']
9
10
11class GroupSerializer(serializers.HyperlinkedModelSerializer):
12 class Meta:
13 model = Group
14 fields = ['url', 'name']
1from django.urls import include, path
2from rest_framework import routers
3from tutorial.quickstart import views
4
5router = routers.DefaultRouter()
6router.register(r'users', views.UserViewSet)
7router.register(r'groups', views.GroupViewSet)
8
9# Wire up our API using automatic URL routing.
10# Additionally, we include login URLs for the browsable API.
11urlpatterns = [
12 path('', include(router.urls)),
13 path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
14]
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]