forked from WasinTh/rest_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
33 lines (25 loc) · 1 KB
/
Copy pathviews.py
File metadata and controls
33 lines (25 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from django.views import View
from django.shortcuts import get_object_or_404
from django.http import JsonResponse
from library.models import Author, Book
from library.sample_2.serializers import AuthorSerializer, BookSerializer
class AuthorList(View):
def get(self, request):
authors = Author.objects.all()
serializer = AuthorSerializer(authors, many=True)
return JsonResponse(serializer.data, safe=False)
class AuthorDetail(View):
def get(self, request, id):
author = get_object_or_404(Author, id=id)
serializer = AuthorSerializer(author)
return JsonResponse(serializer.data)
class BookDetail(View):
def get(self, request, id):
book = get_object_or_404(Book, id=id)
serializer = BookSerializer(book)
return JsonResponse(serializer.data)
class BookList(View):
def get(self, request):
books = Book.objects.all()
serializer = BookSerializer(books, many=True)
return JsonResponse(serializer.data, safe=False)