restframework 的一些操作
路飞学城项目:
1 Vue
2 restframework框架(一周)
3 学城项目(一周-两周)
day98
1 CBV(class based view) 与 FBV(function based view)
class LoginView(View):
def get(self,request):
return render(request,"login.html")
def post(self,request):
user=request.POST.get("user")
pwd=request.POST.get("pwd")
if 1:
return HttpResponse("OK")
url(r'^index/', views.index) # 用户一旦访问:index(request)
# url(r'^login/', views.LoginView.as_view()),
# url(r'^login/', View.as_view()),
# url(r'^login/', View.view),
# 用户一旦访问get请求login-----》
view(request):
return self.dispatch(request, *args, **kwargs)
dispatch(self, request, *args, **kwargs):
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower())
return handler(request, *args, **kwargs)
RESTful:(面向资源架构(ROA:Resource Oriented Architecture))
1 一切皆资源
books books
book/add/ addbook
book/1/change changebook
book/1/delete changebook
restful:
get 查看
books ------------>
post 添加
get 查看
book/1/-------------
delete 删除
put 更新
2
GET :从服务器取出资源(一项或多项)
POST :在服务器新建一个资源
PUT :在服务器更新资源(客户端提供改变后的完整资源)
PATCH :在服务器更新资源(客户端提供改变的属性)
DELETE :从服务器删除资源
restframework框架:
(1) APIView (******)
class PublishView(APIView):
def get(self,request):
publish_list=Publish.objects.all()
ret=serialize("json",publish_list)
return HttpResponse(ret)
def post(self,request):
pass
#url(r'^publishes/', views.PublishView.as_view()),
#url(r'^publishes/', APIView.as_view()),
#url(r'^publishes/', View.view),
用户一旦访问get请求
View.view(request):
return self.dispatch(request, *args, **kwargs)
---- APIView.dispatch(request, *args, **kwargs):
源码
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower())
response = handler(request, *args, **kwargs)
return response
(2) 序列化组件 (******)
将queryset序列化成json
Django序列化组件:
publish_list=Publish.objects.all()
ret=serialize("json",publish_list)
restframework的序列化组件
[obj1,obj2,...]--------> [{},{},{}]
obj--------------------> {}
# 自定义序列化组件
class BookSerializers(serializers.Serializer):
title=serializers.CharField()
price=serializers.CharField()
publishDate=serializers.DateField()
# 针对一对多
publish=serializers.CharField(source="publish.name")
publish_email=serializers.CharField(source="publish.email")
# 针对多对多
authors=serializers.SerializerMethodField()
def get_authors(self,obj):
data=[]
for i in obj.authors.all():
temp=[]
temp.append(i.pk)
temp.append(i.name)
data.append(temp)
return data
# ModelSerializer序列化组件
class BookSerializers(serializers.ModelSerializer):
class Meta:
model=Book
fields="__all__"
publish = serializers.CharField(source="publish.name")
authors=serializers.SerializerMethodField()
def get_authors(self,obj):
data=[]
for i in obj.authors.all():
temp=[]
temp.append(i.pk)
temp.append(i.name)
data.append(temp)
return data
1
"books" GET:获取所有数据
POST:添加一条数据
"book/1" GET:获取单条数据
DELETE:删除一条数据
PUT/PATCH:更新一条数据
2 功能之序列化:
book_list = Book.objects.all()
ps = BookSerializers(book_list, many=True)
ps.data
book_obj=Book.objects.filter(pk=pk).first()
bs=BookSerializers(book_obj,many=False)
3 校验字段与操作记录(ModelSerializer拥有)
# 添加数据
bs=BookSerializers(data=request.data)
if bs.is_valid(): #校验字段接口
bs.save() # 生成记录接口
return Response(bs.data) # 序列化接口
# 更新数据
bs=BookSerializers(data=request.data,instance=book_obj)
if bs.is_valid(): #校验字段接口
bs.save() #更新记录接口
return Response(bs.data) # 序列化接口
(3) 视图组件 (******)
(4) 解析器
(5) 认证组件
(6) 权限组件
(7) 频率组件
(8) 分页器
(9) 响应器
(10) url控制器
(11) 版本控制