From f897583115bb6c52eb2f37f041ef524bf3ad4289 Mon Sep 17 00:00:00 2001 From: halyna-baklanova Date: Sat, 16 Nov 2024 11:58:45 +0200 Subject: [PATCH] Solution --- author/migrations/0001_initial.py | 25 +++++++++++++++++++++++++ author/serializers.py | 14 +++++++++++--- author/tests/tests.py | 2 +- author/urls.py | 14 ++++++++++++-- author/views.py | 7 +++++-- 5 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 author/migrations/0001_initial.py diff --git a/author/migrations/0001_initial.py b/author/migrations/0001_initial.py new file mode 100644 index 0000000..a56a4dd --- /dev/null +++ b/author/migrations/0001_initial.py @@ -0,0 +1,25 @@ +# Generated by Django 4.1 on 2024-11-16 08:40 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Author', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('first_name', models.CharField(max_length=64)), + ('last_name', models.CharField(max_length=64)), + ('pseudonym', models.CharField(blank=True, max_length=64, null=True)), + ('age', models.IntegerField()), + ('retired', models.BooleanField()), + ], + ), + ] diff --git a/author/serializers.py b/author/serializers.py index e516609..7cfe11a 100644 --- a/author/serializers.py +++ b/author/serializers.py @@ -4,19 +4,27 @@ class AuthorSerializer(serializers.ModelSerializer): - id = serializers.IntegerField(read_only=True) + author_id = serializers.IntegerField(read_only=True) first_name = serializers.CharField(max_length=64) last_name = serializers.CharField(max_length=64) pseudonym = serializers.CharField(max_length=64, required=False) age = serializers.IntegerField() retired = serializers.BooleanField() + class Meta: + model = Author + fields = "__all__" + def create(self, validated_data): return Author.objects.create(**validated_data) def update(self, instance, validated_data): - instance.first_name = validated_data.get("first_name", instance.first_name) - instance.last_name = validated_data.get("last_name", instance.last_name) + instance.first_name = validated_data.get( + "first_name", instance.first_name + ) + instance.last_name = validated_data.get( + "last_name", instance.last_name + ) instance.retired = validated_data.get("retired", instance.retired) instance.age = validated_data.get("age", instance.age) instance.retired = validated_data.get("retired", instance.retired) diff --git a/author/tests/tests.py b/author/tests/tests.py index 4bca531..cc836af 100644 --- a/author/tests/tests.py +++ b/author/tests/tests.py @@ -7,7 +7,7 @@ from author.models import Author from author.serializers import AuthorSerializer -AUTHORS_URL = reverse("author:manage-list") +AUTHORS_URL = reverse("author:author-list") class AuthorApiTests(TestCase): diff --git a/author/urls.py b/author/urls.py index fa856a3..632016b 100644 --- a/author/urls.py +++ b/author/urls.py @@ -1,3 +1,13 @@ -# Create your urls here +from django.urls import path, include +from rest_framework import routers -urlpatterns = [] +from author.views import AuthorViewSet + +router = routers.DefaultRouter() +router.register("manage", AuthorViewSet, basename="author") + +urlpatterns = [ + path("", include(router.urls)) +] + +app_name = "author" diff --git a/author/views.py b/author/views.py index 608b89d..60e5d89 100644 --- a/author/views.py +++ b/author/views.py @@ -1,6 +1,9 @@ from rest_framework import viewsets +from author.models import Author +from author.serializers import AuthorSerializer + class AuthorViewSet(viewsets.ModelViewSet): - # write your code here - pass + queryset = Author.objects.all() + serializer_class = AuthorSerializer