通过老师查学生:
先获取老师id,然后查学生表(反向查找) teacher__id ,匹配传过来的老师id,最后序列化展示
通过学生查老师:
先获取学生id,然后查老师表(外键)stu,匹配传过来的学生id,最后序列化展示
class ShowTea(APIView):
def get(self,request):
tea = Teacher.objects.all()
ser = TeaSer(tea, many=True)
return Response({
'code': 200,
'data': ser.data
})
class Shows(APIView):
def post(self,request):
id = request.data.get('id')
stu = Student.objects.filter(teacher__id=id)
ser = StudentSer(stu,many=True)
return Response(ser.data)
class Showtt(APIView):
def post(self,request):
id = request.data.get('id')
tea = Teacher.objects.filter(stu=id)
tea = TeacherSer(stu,many=True)
return Response(ser.data)
<template>
<div>
<table border="1">
<tr>
<th>老师</th>
</tr>
<tr v-for="item in datalist">
<td><button @click="shos(item.id)">{{ item.name }}</button></td>
</tr>
<div v-for="i in ss">
<button @click="ttt(i.id)">{{ i.name }}</button>
</div>
<li v-for="i in tt">{{ i.name }}</li>
</table>
</div>
</template>
<script>
import axios from 'axios'
export default {
data(){
return{
datalist:[],
ss:[],
tt:[]
}
},
mounted() {
axios({
url:'http://localhost:8000/showtea/',
method:'get'
}).then(res=>{
console.log(res);
this.datalist = res.data.data;
})
},
methods:{
shos(id){
axios({
url:'http://localhost:8000/shows/',
method:'post',
data:{'id':id}
}).then(res=>{
console.log(res);
this.ss = res.data
})
},
ttt(id){
axios({
url:'http://localhost:8000/showtt/',
method:'post',
data:{'id':id}
}).then(res=>{
console.log(res);
this.tt = res.data
})
}
}
}
</script>
<style scoped>
</style>