RESTFramework(DRF)进阶篇(APIView类)
Posted 运维-大白同学
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RESTFramework(DRF)进阶篇(APIView类)相关的知识,希望对你有一定的参考价值。
1. DRF序列化器:关联表显示
应用场景:
一对多:一个项目有多个应用,一个应用只能属于一个项目
多对多:一个应用部署到多台服务器,一个服务器部署多个应用
1.1 定义数据模型
class Project(models.Model):
name = models.CharField(max_length=30)
class App(models.Model):
name =models.CharField(max_length=30)
Project = models.ForeignKey(Project,on_delete=models.CASCADE) #一对多ForeignKey
class Server(models.Model):
hostname = models.CharField(max_length=30)
ip = models.GenericIPAddressField()
app = models.ManyToManyField(App) #多对多ManyToManyField
1.2 定义路由
re_path('^api/project/$', views.ProjectView.as_view()),
re_path('api/app/$',views.Appview.as_view()),
re_path('^api/server/$',views.ServerView.as_view()),
1.3 定义序列化器(serializers.py)
from myapp_api.models import User
from myapp_api.models import Project, App, Server
from rest_framework import serializers
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = "__all__"
extra_kwargs =
'name': 'max_length':2, 'required':True, #限制名字长度
'age':'min_value':16,'max_value':100,'required':True #限制年龄
class ProjectSerializer(serializers.ModelSerializer):
class Meta:
model = Project
fields = "__all__"
class AppSerializer(serializers.ModelSerializer):
class Meta:
model = App
fields = "__all__"
class ServerSerializer(serializers.ModelSerializer):
class Meta:
model = Server
fields = "__all__"
1.4 定义视图(views)
from rest_framework.views import APIView
from rest_framework.response import Response
from .serializers import UserSerializer, ProjectSerializer,AppSerializer,ServerSerializer
from myapp_api.models import User, Project, App, Server
class Appview(APIView):
def get(self, request, id=None):
if id:
obj = App.objects.get(id=id)
ser = AppSerializer(obj)
else:
queryset = App.objects.all()
ser = AppSerializer(queryset, many=True)
res = 'code': 200, 'msg': '获取成功', 'data': ser.data
return Response(res)
def post(self,request):
ser = AppSerializer(data=request.data)
ser.is_valid(raise_exception=True)
ser.save()
res = 'code': 200, 'msg': '创建用户成功'
return Response(res)
class ServerView(APIView):
def get(self, request, id=None):
if id:
obj = Server.objects.get(id=id)
ser = ServerSerializer(obj)
else:
queryset = Server.objects.all()
ser = ServerSerializer(queryset, many=True)
res = 'code': 200, 'msg': '获取成功', 'data': ser.data
return Response(res)
def post(self,request):
ser = ServerSerializer(data=request.data)
ser.is_valid(raise_exception=True)
ser.save()
res = 'code': 200, 'msg': '创建用户成功'
return Response(res)
1.5添加数据
project/app/server/分别可以成功添加数据
2.0 DRF 序列化器:关联表显示
序列化器返回时当前模型中的字段,如果字段时外键时,返回是外键对应id,如果想要显示外键对应的详细信息做法如下
3.0 DRF序列化器 SerializerMethodFuekd
统计该项目应用个数
DRF 序列化器: 改变序列化和反序列化的行为
4.0 Request与Response
DRF传入视图的request对象不再是Django默认的HttpRequest对象,而是基于HttpRequest类扩展后的Request类的对象,Request对象的数据是自动根据前端发送的数据同意解析数据格式
常用属性:
request.data: 返回post提交的数据,与request.post类似
request.query_params:返回GET URL 参数,与request.GET类似
以上是关于RESTFramework(DRF)进阶篇(APIView类)的主要内容,如果未能解决你的问题,请参考以下文章
RESTFramework(DRF)进阶篇GenericAPIview-ViewSet类
RESTFramework(DRF)进阶篇GenericAPIview-ViewSet类
RESTFramework(DRF)框架初探以及认识serializers序列化器的增删改查