视图plus
from django.shortcuts import render
from myapp.models import Province,Student
from rest_framework.views import APIView
from myapp.serializers import ProvinceSerializers,StudentSerializers,StudentSerializers2
from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response
class ProvinceView(ModelViewSet):
queryset = Province.objects.all()
serializer_class = ProvinceSerializers
class StudentView(ModelViewSet):
queryset = Student.objects.all()
serializer_class = StudentSerializers
class StudentView2(APIView):
def get(self,request,pk):
try:
stu_data = Student.objects.filter(province_id=pk)
except Exception as e:
print(e)
return Response({'msg':'error'},status=404)
ser = StudentSerializers2(stu_data,many=True)
return Response(ser.data,status=200)
路由plus
from django.urls import path
from myapp import views
from rest_framework import routers
urlpatterns = [
path('student2/<int:pk>/',views.StudentView2.as_view())
]
router = routers.SimpleRouter()
router.register('province',views.ProvinceView,'province')
router.register('student',views.StudentView,'student')
urlpatterns +=router.urls