其他分享
首页 > 其他分享> > drf快速生成5个基本的api接口

drf快速生成5个基本的api接口

作者:互联网

序列化器serializer:

from rest_framework import serializers
from .models import Student

class StudentModelSerializer(serializers.ModelSerializer):
    class Meta:
        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

  

 

路由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
来源: https://www.cnblogs.com/fuck-Innovative-training/p/16638870.html