序列化器serializer:
from rest_framework import serializers from .models import Student class StudentModelSerializer(serializers.ModelSerializer): class Meta: model = Student fields = "__all__"
- model 指明该序列化器处理的数据字段从模型类Student参考生成
- fields 指明该序列化器包含模型类中的哪些字段,’all‘指明包含所有字段
视图views:
from rest_framework.viewsets import ModelViewSet from .models import Student from .serializers import StudentModelSerializer class StudentViewSet(ModelViewSet): queryset = Student.objects.all() serializer_class = StudentModelSerializer
- queryset 指明该视图集在查询数据时使用的查询集
- serializer_class 指明该视图在进行序列化或反序列化时使用的序列化器
路由urls:
from . import views from rest_framework.routers import DefaultRouter # 路由列表 urlpatterns = [ ] router = DefaultRouter() # 可以处理视图的路由器 router.register('students', views.StudentViewSet) # 向路由器中注册视图集 urlpatterns += router.urls # 将路由器中的所以路由信息追到到django的路由列表中
标签:接口,api,Student,指明,import,序列化,class,路由,drf From: https://www.cnblogs.com/fuck-Innovative-training/p/16638870.html