-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7155dc0
commit 7920b8d
Showing
3 changed files
with
65 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |