from django.contrib.auth.models import User, Group from rest_framework import viewsets from tutorial.quickstart.serializers import UserSerializer, GroupSerializer
classUserViewSet(viewsets.ModelViewSet): """ API endpoint that allows users to be viewed or edited. """ queryset = User.objects.all().order_by('-date_joined') serializer_class = UserSerializer
classGroupViewSet(viewsets.ModelViewSet): """ API endpoint that allows groups to be viewed or edited. """ queryset = Group.objects.all() serializer_class = GroupSerializer
URLs
url(tutorial/urls.py)を定義。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
from django.urls import include, path from rest_framework import routers from tutorial.quickstart import views
# Wire up our API using automatic URL routing. # Additionally, we include login URLs for the browsable API. urlpatterns = [ path('', include(router.urls)), path('api-auth/', include('rest_framework.urls', namespace='rest_framework')) ]
Pagination
settings(tutorial/settings.py)をカスタマイズ。
SECRET_KEYを環境変数で定義する
INSTALL_APPSでrest_frameworkを追加する
Pagenation(次のページ)を有効化する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = os.environ['SECRET_KEY']