ModelViewSet + ModelSerializer
ModelSerializer
(封装好的序列化器,不需要我们写字段)
from rest_framework import serializers from .models import * class UserSer(serializers.ModelSerializer): class Meta: model = User # 表名 fields = '__all__' # 获取所有信息 depth = 1 # 序列化外键 (一层序列一层)
ModelViewSet
(封装好的drf框架,不需要我们自己写增删改查)
from rest_framework.viewsets import ModelViewSet class UserModelView(ModelViewSet): queryset = User.objects.all() # 获取表中所有数据 serializer_class = UserSer # 序列化操作增删改查