From 23ea030f8a4316d6d4cb9fcf27dd951397a1724f Mon Sep 17 00:00:00 2001 From: JasonHomeWorkstationUbuntu Date: Wed, 21 Oct 2020 10:13:30 +1100 Subject: [PATCH] Finished Quickstart tutorial --- src/quickstart/serializers.py | 14 ++++++++++++++ src/quickstart/views.py | 22 +++++++++++++++++++++- src/tutorial/settings.py | 7 +++++++ src/tutorial/urls.py | 19 +++++++++++++++++-- 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/quickstart/serializers.py diff --git a/src/quickstart/serializers.py b/src/quickstart/serializers.py new file mode 100644 index 0000000..adec23d --- /dev/null +++ b/src/quickstart/serializers.py @@ -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'] \ No newline at end of file diff --git a/src/quickstart/views.py b/src/quickstart/views.py index 91ea44a..57dea60 100644 --- a/src/quickstart/views.py +++ b/src/quickstart/views.py @@ -1,3 +1,23 @@ 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. diff --git a/src/tutorial/settings.py b/src/tutorial/settings.py index a603c9f..0d35bd1 100644 --- a/src/tutorial/settings.py +++ b/src/tutorial/settings.py @@ -37,6 +37,8 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + # 3rd party + 'rest_framework' ] MIDDLEWARE = [ @@ -118,3 +120,8 @@ USE_TZ = True # https://docs.djangoproject.com/en/3.1/howto/static-files/ STATIC_URL = '/static/' + +REST_FRAMEWORK = { + 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', + 'PAGE_SIZE': 10 +} \ No newline at end of file diff --git a/src/tutorial/urls.py b/src/tutorial/urls.py index 52d38c1..7280e29 100644 --- a/src/tutorial/urls.py +++ b/src/tutorial/urls.py @@ -14,8 +14,23 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ 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 = [ - path('admin/', admin.site.urls), + path('', include(router.urls)), + path('api-auth/', include('rest_framework.urls', namespace='rest_framework')) ]