DRF作业
题目
(1)用postman,用django写几个接口,测试,导出文件
(2)新建一个图书表,5个符合restful规范的接口,用CBV的APIView实现
(3)rest_framework的Resquest类和APIView类,流程,你走一遍,整理成自己的话
-
新建一个图书表,5个符合restful规范的接口,用CBV 的APIView实现
(1)GET请求获取数据
# 获取所有的数据 GET http://127.0.0.1:8004/books/
# 获取一条存在的数据 GET http://127.0.0.1:8004/books/3/
# book_id不存在 GET http://127.0.0.1:8004/books/13/
-
post请求增加一条数据
# 增加一条数据 POST http://127.0.0.1:8004/books/
json格式数据
{ "name":"xxxx魔方手册", "author":"alex", "price":"160.0", "publish":"中华出版社", "publish_time":"2010-08-06T18:36:20Z" }
# 增加数据失败 POST http://127.0.0.1:8004/books/
json格式数据
{ "name":"魔方手册", "author":"alex", "publish_time":"2018-08-06T18:36:20Z" }
-
put请求更新数据
# 更新一条数据 PUT http://127.0.0.1:8004/books/1/
json格式数据
{ "name": "那些年egon追过的富婆cc", "author": "lqz", "price": "180.0", "publish": "中华出版社", "publish_time": "2020-05-26T16:35:51Z" }
# 没有携带book_id参数 PUT http://127.0.0.1:8004/books/
# 使用不存在的book_id PUT http://127.0.0.1:8004/books/11/
-
delete请求删除数据
# 没有携带book_id DELETE http://127.0.0.1:8004/books/
# 携带不不存在的book_id DELETE http://127.0.0.1:8004/books/11/
# 携带一条正确的book_id DELETE http://127.0.0.1:8004/books/4/
-
patch使用方式同put
models.py
from django.db import models
# Create your models here.
class Book(models.Model):
name = models.CharField(max_length=32, verbose_name='书名')
author = models.CharField(max_length=32, verbose_name='作者')
price = models.DecimalField(max_digits=5, decimal_places=1, verbose_name='价格')
publish = models.CharField(max_length=32, verbose_name='出版社名称')
publish_time = models.DateTimeField(auto_now_add=True, verbose_name='出版时间')
def __str__(self):
return self.name
class Meta:
verbose_name_plural = '书籍表'
urls.py
from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('books/', views.Books.as_view(), name='books'),
path('books/<int:book_id>/', views.Books.as_view(), name='books'),
]
views.py
from django.http import JsonResponse
# Create your views here.
from rest_framework.views import APIView
from app01 import models
import json
class Books(APIView):
@property
def book_id_error(self):
return [{'status': 404, 'msg': 'book_id不存在'}]
@staticmethod
def data_process(queryset_obj):
re_list = []
for item in queryset_obj:
re_dict = {}
re_dict.update(item.__dict__)
re_dict.pop('_state')
re_list.append(re_dict)
return re_list
def get(self, request, book_id=None):
if not book_id:
book_queryset = models.Book.objects.all()
else:
book_queryset = models.Book.objects.filter(pk=book_id)
re_list = self.data_process(book_queryset) if book_queryset else self.book_id_error
return JsonResponse(re_list, safe=False)
def post(self, request):
name = request.data.get('name')
try:
models.Book.objects.create(**request.data)
book_queryset = models.Book.objects.filter(name=name)
re_list = self.data_process(book_queryset)
except Exception as e:
re_list = [{'status': 404, 'msg': e.args[1].split(' ')[1] + '参数不能为空'}]
return JsonResponse(re_list, safe=False)
def put(self, request, book_id=None):
if not book_id:
re_list = [{'status': 404, 'msg': '缺少关键性参数book_id'}]
else:
book_queryset = models.Book.objects.filter(pk=book_id)
if book_queryset:
models.Book.objects.update(**request.data)
book_queryset = models.Book.objects.filter(pk=book_id)
re_list = self.data_process(book_queryset)
else:
re_list = self.book_id_error
return JsonResponse(re_list, safe=False)
def delete(self, request, book_id=None):
if not book_id:
re_list = [{'status': 404, 'msg': '缺少关键性参数book_id'}]
else:
book_queryset = models.Book.objects.filter(pk=book_id)
if book_queryset:
book_queryset.delete()
re_list = [{'status': 200, 'msg': '删除成功'}]
else:
re_list = self.book_id_error
return JsonResponse(re_list, safe=False)
def patch(self, request, book_id):
if not book_id:
re_list = [{'status': 404, 'msg': '缺少关键性参数book_id'}]
else:
book_queryset = models.Book.objects.filter(pk=book_id)
if book_queryset:
models.Book.objects.update(**request.data)
book_queryset = models.Book.objects.filter(pk=book_id)
re_list = self.data_process(book_queryset)
else:
re_list = self.book_id_error
return JsonResponse(re_list, safe=False)