Finished Quickstart tutorial

master
Jason Zhu 2020-10-21 10:13:30 +11:00
parent 6f99499f1a
commit 23ea030f8a
4 changed files with 59 additions and 3 deletions

View File

@ -0,0 +1,14 @@
from django.contrib.auth.models import User, Group
from rest_framework import serializers
# Here is data representation for serializer
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ['url', 'username', 'email', 'groups']
class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Group
fields = ['url', 'names']

View File

@ -1,3 +1,23 @@
from django.shortcuts import render from django.shortcuts import render
from rest_framework import viewsets
from rest_framework import permissions
from django.contrib.auth.models import User, Group
from .serializers import UserSerializer, GroupSerializer
class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited. (Group all common user behavior into this UserViewSet)
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
permission_classes = [permissions.IsAuthenticated]
class GroupViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited. (Group all common Group behavior into this GroupViewSet)
"""
queryset = Group.objects.all()
serializer_class = GroupSerializer
permission_classes = [permissions.IsAuthenticated]
# Create your views here.

View File

@ -37,6 +37,8 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
# 3rd party
'rest_framework'
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@ -118,3 +120,8 @@ USE_TZ = True
# https://docs.djangoproject.com/en/3.1/howto/static-files/ # https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/' STATIC_URL = '/static/'
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10
}

View File

@ -14,8 +14,23 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
from django.contrib import admin from django.contrib import admin
from django.urls import path from django.urls import path, include
from rest_framework import routers
from quickstart import views
# Because we're using viewsets instead of views, we can automatically generate the URL conf for our API, by simply registering the viewsets with a router class.
# Again, if we need more control over the API URLs we can simply drop down to using regular class-based views, and writing the URL conf explicitly.
# Finally, we're including default login and logout views for use with the browsable API. That's optional, but useful if your API requires authentication and you want to use the browsable API.
router = routers.DefaultRouter() # ??
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
] ]