From 8f70b64c98c2b3e4bf414b5ebbbe61d84294a751 Mon Sep 17 00:00:00 2001 From: halyna-baklanova Date: Sat, 16 Nov 2024 01:19:27 +0200 Subject: [PATCH] create serializers for Author --- author/serializers.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/author/serializers.py b/author/serializers.py index d286b95..e516609 100644 --- a/author/serializers.py +++ b/author/serializers.py @@ -1,6 +1,24 @@ from rest_framework import serializers +from author.models import Author + class AuthorSerializer(serializers.ModelSerializer): - # write your code here - pass + 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() + + 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.retired = validated_data.get("retired", instance.retired) + instance.age = validated_data.get("age", instance.age) + instance.retired = validated_data.get("retired", instance.retired) + instance.save() + return instance