Skip to content

Commit

Permalink
create serializers for Author
Browse files Browse the repository at this point in the history
  • Loading branch information
halyna-baklanova committed Nov 15, 2024
1 parent 7155dc0 commit 8f70b64
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions author/serializers.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8f70b64

Please sign in to comment.