From 095003b9430921854b18095317eab454cd6d91d2 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Mon, 6 Jan 2020 22:36:12 +0000 Subject: [PATCH] Enable basic routing of /users --- apiserver/apiserver/api/serializers.py | 7 +++++++ apiserver/apiserver/api/views.py | 9 +++++++-- apiserver/apiserver/urls.py | 24 ++++++++---------------- 3 files changed, 22 insertions(+), 18 deletions(-) create mode 100644 apiserver/apiserver/api/serializers.py diff --git a/apiserver/apiserver/api/serializers.py b/apiserver/apiserver/api/serializers.py new file mode 100644 index 0000000..c1a712a --- /dev/null +++ b/apiserver/apiserver/api/serializers.py @@ -0,0 +1,7 @@ +from django.contrib.auth.models import User, Group +from rest_framework import serializers + +class UserSerializer(serializers.ModelSerializer): + class Meta: + model = User + fields = ['id', 'username', 'email', 'groups'] diff --git a/apiserver/apiserver/api/views.py b/apiserver/apiserver/api/views.py index 91ea44a..a7befe8 100644 --- a/apiserver/apiserver/api/views.py +++ b/apiserver/apiserver/api/views.py @@ -1,3 +1,8 @@ -from django.shortcuts import render +from django.contrib.auth.models import User, Group +from rest_framework import viewsets -# Create your views here. +from . import models, serializers + +class UserViewSet(viewsets.ModelViewSet): + queryset = User.objects.all().order_by('-date_joined') + serializer_class = serializers.UserSerializer diff --git a/apiserver/apiserver/urls.py b/apiserver/apiserver/urls.py index 4fd2238..503f081 100644 --- a/apiserver/apiserver/urls.py +++ b/apiserver/apiserver/urls.py @@ -1,21 +1,13 @@ -"""apiserver URL Configuration - -The `urlpatterns` list routes URLs to views. For more information please see: - https://docs.djangoproject.com/en/3.0/topics/http/urls/ -Examples: -Function views - 1. Add an import: from my_app import views - 2. Add a URL to urlpatterns: path('', views.home, name='home') -Class-based views - 1. Add an import: from other_app.views import Home - 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') -Including another URLconf - 1. Import the include() function: from django.urls import include, path - 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 include, path +from rest_framework import routers + +from .api import views + +router = routers.DefaultRouter() +router.register(r'users', views.UserViewSet) urlpatterns = [ + path('', include(router.urls)), path('admin/', admin.site.urls), ]