django 关联表查询
程序员文章站
2022-04-17 08:03:51
...
django 关联表查询
1.新建serializers.py文件
from rest_framework import serializers
from .models import Grade,Course,Student,OnLine
class StudentSerialize(serializers.ModelSerializer):
class Meta:
model = Student # 要查找的表
fields = '__all__' # 查询内容 __all__表示所有
depth = 1 # 层级 表示查几层
2. view.py 内容
from .models import Grade,Course,Student,OnLine
from .serializers import OnLineSerialize
class orm(APIView):
def get(self,request):
dataList = OnLine.objects.all()
ret = OnLineSerialize(dataList,many=True)
return Response(ret.data)