From e77b9ede45e2f017da3f4dfb723a8ef835687a5b Mon Sep 17 00:00:00 2001 From: Mingyu Park Date: Sun, 18 Aug 2024 12:10:41 +0900 Subject: [PATCH] add category url & view --- backend/categories/serializers.py | 8 ++++++++ backend/categories/urls.py | 20 +++++++++++++++++++- backend/categories/views.py | 16 ++++++---------- 3 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 backend/categories/serializers.py diff --git a/backend/categories/serializers.py b/backend/categories/serializers.py new file mode 100644 index 0000000..b0548e1 --- /dev/null +++ b/backend/categories/serializers.py @@ -0,0 +1,8 @@ +from rest_framework import serializers +from .models import Category + +class CategorySerializer(serializers.ModelSerializer): + + class Meta: + model = Category + fields = "__all__" \ No newline at end of file diff --git a/backend/categories/urls.py b/backend/categories/urls.py index 4bfdf2d..6af068e 100644 --- a/backend/categories/urls.py +++ b/backend/categories/urls.py @@ -2,5 +2,23 @@ from . import views urlpatterns = [ - path("", views.categories), + path( + "", + views.CategoryViewSet.as_view( + { + "get": "list", + "post": "create", + } + ), + ), + path( + "", + views.CategoryViewSet.as_view( + { + "get": "retrieve", + "put": "partial_update", + "delete": "destroy", + } + ), + ), ] \ No newline at end of file diff --git a/backend/categories/views.py b/backend/categories/views.py index c1c7e75..c480c65 100644 --- a/backend/categories/views.py +++ b/backend/categories/views.py @@ -1,12 +1,8 @@ -from rest_framework.decorators import api_view -from rest_framework.response import Response +from rest_framework.viewsets import ModelViewSet from .models import Category +from .serializers import CategorySerializer -@api_view() -def categories(request): - return Response( - { - "ok": True, - "categories": Category.objects.all() - } - ) \ No newline at end of file +class CategoryViewSet(ModelViewSet): + + serializer_class = CategorySerializer + queryset = Category.objects.all() \ No newline at end of file