# 将程序中的一个数据结构类型转换为其他格式(字典、JSON、XML等),例如将Django中的模型类对象装换为JSON字符串,这个转换过程我们称为序列化
# 将其他格式(字典、JSON、XML等)转换为程序中的数据,例如将JSON字符串转换为Django中的模型类对象,这个过程我们称为反序列化
django开发rest接口
# 获取所有图书信息接口 GET /tushu/
# 增加图书 POST /tushu/
参数为json格式
状态码 201
成功返回增加的数据内容
# /tushu/3/ 路径上有参数
查询单个 GET
修改某条数据 PUT
删除某条数据 DELETE
# 新建tushu模型表 models.py
from django.db import models
# Create your models here.
from django.db.models import Model
class TuShu(Model):
tid = models.AutoField(primary_key=True, verbose_name="图书id")
name = models.CharField(max_length=20, verbose_name="图书名称")
riqi = models.DateField(null=True, verbose_name="上架日期")
count = models.IntegerField(default=0, verbose_name="数量")
id_delete = models.BooleanField(default=False, verbose_name="是否删除")
class Meta:
db_table = "tushu"
verbose_name = "图书"
def __str__(self):
return self.name
'''
python manage.py makemigrations # 根据模型类生成建表的语句
python manage.py migrate # 创建表
'''
# 创建视图函数
import json
from django.http import JsonResponse, HttpResponse
from django.shortcuts import render
# Create your views here.
from django.views import View
from tushu.models import TuShu
'''
GET /tushu/ /tushu2/2/
POST /tushu/
PUT /tushu/2/
DELETE /tushu/2/
'''
class TuShuView(View):
# 查询所有
def get(self, request):
# 所有图书 结果集
qset = TuShu.objects.all()
# 转成json字典格式,序列化操作
list = []
for q in qset:
list.append({
"tid": q.tid,
"name": q.name,
"riqi": q.name,
"count": q.count,
"is_delete": q.id_delete
})
dict = {"code": 0, "error": "返回成功", "list": list}
return JsonResponse(data=dict) # safel=False:关闭校验字典格式的安全模式
def post(self, request):
# 1. 参数转成字典格式
data = json.loads(request.body)
print("---------data=", data)
# 2. 参数校验
# 3. 添加数据
q = TuShu.objects.create(name=data.get("name"), riqi=data.get("riqi"),
count=data.get("count"), id_delete=data.get("id_delete"))
# 规范要求 增加成功之后返回增加的数据对象 状态码为201:已创建
return JsonResponse(data={
"tid": q.tid,
"name": q.name,
"riqi": q.riqi,
"count": q.count,
"id_delete": q.id_delete
}, status=201) # safe=False
class TuShuView2(View):
# 查询单个图书接口
def get(self, request, tid):
try:
q = TuShu.objects.get(tid=tid)
except Exception:
return JsonResponse(data={}, status=404)
return JsonResponse(data={
"tid": q.tid,
"name": q.name,
"riqi": q.riqi,
"count": q.count,
"id_delete": q.id_delete
}, )
# 修改信息
def put(self, request, tid):
try:
q = TuShu.objects.get(tid=tid)
except Exception:
return JsonResponse(data={}, status=404)
# 获取请求参数
data = json.loads(request.body)
q.name = data.get("name")
q.riqi = data.get("riqi")
q.count = data.get("count")
q.id_delete = data.get("id_delete")
# 保存
q.save()
return JsonResponse(data={
"tid": q.tid,
"name": q.name,
"riqi": q.riqi,
"count": q.count,
"id_delete": q.id_delete
}, status=201)
# 根据tid删除
def delete(self, request, tid):
try:
q = TuShu.objects.get(tid=tid)
except Exception:
return JsonResponse(data={}, status=404)
# 执行删除
q.delete()
# return JsonResponse(status=204) # JsonResponse需要data参数
# 删除成功后,要求返回空白文档 状态204
return HttpResponse(status=204)
# 创建路由
urlpatterns = [
# 用于get所有,post增加
path('tushu/', TuShuView.as_view()),
# 用于get查找, put修改,delete删除
path('tushu/<tid>/', TuShuView2.as_view()),
]