Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergunshot committed Dec 3, 2024
1 parent 7155dc0 commit e1b01be
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
12 changes: 12 additions & 0 deletions author/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,15 @@ class Author(models.Model):
pseudonym = models.CharField(max_length=64, null=True, blank=True)
age = models.IntegerField()
retired = models.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.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
26 changes: 23 additions & 3 deletions author/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
from rest_framework import serializers

from author.models import Author

class AuthorSerializer(serializers.ModelSerializer):
# write your code here
pass

class AuthorSerializer(serializers.Serializer):
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.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
14 changes: 12 additions & 2 deletions author/urls.py
Original file line number Diff line number Diff line change
@@ -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("authors", AuthorViewSet, basename="manage")

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

app_name = "author"
13 changes: 9 additions & 4 deletions author/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from rest_framework import viewsets
from rest_framework import viewsets, mixins

from author.models import Author
from author.serializers import AuthorSerializer

class AuthorViewSet(viewsets.ModelViewSet):
# write your code here
pass

class AuthorViewSet(
viewsets.ModelViewSet,
):
queryset = Author.objects.all()
serializer_class = AuthorSerializer

0 comments on commit e1b01be

Please sign in to comment.