Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
arsen-arutiunov committed Nov 5, 2024
1 parent 7155dc0 commit 7920b8d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
35 changes: 33 additions & 2 deletions author/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
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()

class Meta:
model = Author
fields = ["id",
"first_name",
"last_name",
"pseudonym",
"age",
"retired"]

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.pseudonym = validated_data.get("pseudonym",
instance.pseudonym)
instance.age = validated_data.get("age", instance.age)
instance.retired = validated_data.get("retired", instance.retired)
instance.save()
return instance
28 changes: 26 additions & 2 deletions author/urls.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
# 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("authors", AuthorViewSet, basename="author-list")

author_list = AuthorViewSet.as_view(
actions={"get": "list",
"post": "create"}
)

author_detail = AuthorViewSet.as_view(
actions={"get": "retrieve",
"put": "update",
"patch": "partial_update",
"delete": "destroy"}
)

urlpatterns = [
path("", include(router.urls)),
]

app_name = "author"
8 changes: 6 additions & 2 deletions author/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
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

0 comments on commit 7920b8d

Please sign in to comment.