drf——数据库用户信息查询接口
1.models.py-->执行数据迁移命令
ps:模型层中可以加其他的,只要不是Field的字类,都不会当作字段去处理
from django.db import models class Student(models.Model): name = models.CharField(max_length=32) sex = models.SmallIntegerField(choices=((1, '男'), (2, '女'), (3, '未知')), default=1) # hobby=models.CharField(choices=[('1','篮球'),('2','足球')]) age = models.IntegerField()
2.serializer.py 视图类
from rest_framework import serializers from app01.models import Student class StudentSerializer(serializers.ModelSerializer):#Meta方法 class Meta: model = Student fields='__all__'
3.settings.py
INSTALLED_APPS = [ ... 'app01', 'rest_framework' ]
4.urls.py
from django.contrib import admin from django.urls import path from app01 import views from rest_framework.routers import SimpleRouter router=SimpleRouter() router.register('students',views.StudentView) urlpatterns = [ path('admin/', admin.site.urls), # path('students/', views.StudentView.as_view()), # 如果视图函数继承了ViewSetMixin或者它的字类,路由方式必须如下 # path('students/', views.StudentView.as_view({'get':'get'})), # 继承GenericViewSet, ListModelMixin,用此路由 # path('students/', views.StudentView.as_view({'get':'list'})), ] urlpatterns+=router.urls
5.views.py视图类
from django.shortcuts import render # 显示所有学生的接口 from app01.models import Student from app01.serializer import StudentSerializer from rest_framework.views import APIView from rest_framework.generics import GenericAPIView, ListAPIView from rest_framework.mixins import ListModelMixin from rest_framework.viewsets import ViewSetMixin, ViewSet, GenericViewSet from rest_framework.response import Response ## 方式一 # class StudentView(GenericAPIView, ListModelMixin): # queryset = Student.objects.all() # serializer_class = StudentSerializer # # def get(self, request, *args, **kwargs): # return self.list(request, *args, **kwargs) # 方式二 # class StudentView(ListAPIView): # queryset = Student.objects.all() # serializer_class = StudentSerializer # 方式三 # # class StudentView(ViewSetMixin, APIView): # class StudentView(ViewSet): # 跟上行完全一样 # def get(self, request, *args, **kwargs): # student_list = Student.objects.all() # ser = StudentSerializer(student_list, many=True) # # return Response(ser.data) # 方式四 # class StudentView(ViewSetMixin, GenericAPIView): # class StudentView(GenericViewSet): # 跟上行完全一样 # queryset = Student.objects.all() # serializer_class = StudentSerializer # def get(self,request, *args, **kwargs): # obj_list=self.get_queryset() # ser=self.get_serializer(obj_list,many=True) # return Response(ser.data) # 方式5 # class StudentView(GenericViewSet,ListModelMixin): # queryset = Student.objects.all() # serializer_class = StudentSerializer # def get(self,request, *args, **kwargs): # return self.list(request, *args, **kwargs) # 方式6 # class StudentView(GenericViewSet, ListAPIView): # queryset = Student.objects.all() # serializer_class = StudentSerializer # 方式7 # class StudentView(GenericViewSet, ListModelMixin): # queryset = Student.objects.all() # serializer_class = StudentSerializer # 没有get方法,那就把路由配置中get方法变成list,如果要用自己配的路由,那就要写get方法,详看方式8 # 方式8 # class StudentView(GenericViewSet, ListModelMixin): # queryset = Student.objects.all() # serializer_class = StudentSerializer # def get(self,request, *args, **kwargs): # return self.list(request, *args, **kwargs) # 方式9 可自动生成路由 # class StudentView(GenericViewSet, ListAPIView): # class StudentView(ViewSetMixin, ListAPIView): # queryset = Student.objects.all() # serializer_class = StudentSerializer ## 最后常用方案,自动生成路由 # 方式10 :查询所有学生和新增一个学生 (这里记得新增时候serializer视图类中不能有字段重写的 get_字段名_display 方法,不然会报错) from rest_framework.generics import CreateAPIView # class StudentView(ViewSetMixin, ListAPIView,CreateAPIView): # queryset = Student.objects.all() # serializer_class = StudentSerializer