added users & groups view

This commit is contained in:
Chneemann 2024-08-01 13:54:49 +02:00
parent 2bfe84f881
commit 3ba66f282a
4 changed files with 47 additions and 3 deletions

View file

@ -0,0 +1,12 @@
from django.contrib.auth.models import User, Group
from rest_framework import serializers
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ['url', 'id', 'username', 'email']
class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Group
fields = ['url', 'name']

View file

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

View file

@ -20,7 +20,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-hyqc43^!fqs^t_qgs0eycn-ukig$=@ac$wz-ix2n4*53ybmgri'
SECRET_KEY = 'django-insecure-l88ot$w3&q2v439@j_5ps()@1*fa&ho1o=eiig9##0!#3mz9nr'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
@ -30,6 +30,11 @@ ALLOWED_HOSTS = []
# Application definition
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10
}
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
@ -37,6 +42,8 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'users',
]
MIDDLEWARE = [

View file

@ -15,8 +15,16 @@ 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 include, path
from rest_framework import routers
from users import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]