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
55 lines (47 loc) · 1.57 KB
/
Copy pathviews.py
File metadata and controls
55 lines (47 loc) · 1.57 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import json
from django.http import HttpResponse
from django.views import View
from django.shortcuts import get_object_or_404
from library.models import Author, Book
class AuthorList(View):
def get(self, request):
response = list()
for author in Author.objects.all():
response.append({
'id': author.id,
'name': author.name
})
return HttpResponse(json.dumps(response), content_type='application/json')
class AuthorDetail(View):
def get(self, request, id):
author = get_object_or_404(Author, id=id)
response = {
'id' : author.id,
'name': author.name
}
return HttpResponse(json.dumps(response), content_type='application/json')
class BookDetail(View):
def get(self, request, id):
book = get_object_or_404(Book, id=id)
response = {
'id': book.id,
'name': book.name,
'author': {
'id': book.author.id,
'name': book.author.name
}
}
return HttpResponse(json.dumps(response), content_type='application/json')
class BookList(View):
def get(self, request):
response = list()
for book in Book.objects.all():
response.append({
'id': book.id,
'name': book.name,
'author': {
'id': book.author.id,
'name': book.author.name
}
})
return HttpResponse(json.dumps(response), content_type='application/json')