axios封装和GenericAPIView结合混⼊类
class ListMixin:
def get(self, request):
ser = self.serializer_class(self.get_queryset(), many=True)
return Response(ser.data)
class TypeView(APIView, ListMixin):
serializer_class = TypeSer
def get_queryset(self):
queryset = Type.objects.all()
return queryset
class BookView(APIView, ListMixin):
serializer_class = BookSer
def get_queryset(self):
queryset = Book.objects.all()
return queryset
GenericAPIView
from rest_framework.generics import GenericAPIView
class List(GenericAPIView):
'''
获取单个全部
'''
serializer_class = UserSerializers
queryset = User.objects.all()
lookup_url_kwarg = 'id'
lookup_field = 'pk'
def get(self,request,id):
data = self.get_object()
ser = self.get_serializer(instance = data)
return Response(ser.data)
获取全部数据
class List(GenericAPIView):
'''
获取全部
'''
serializer_class = UserSerializers
queryset = User.objects.all()
def get(self,request,id):
data = self.get_queryset()
ser = self.get_serializer(instance = data)
return Response(ser.data)
GenericAPIView结合混⼊类
from bookapp.serializers import BookSerializers
from rest_framework.generics import GenericAPIView
from apps.bookapp.models import Book
from rest_framework.mixins import CreateModelMixin,DestroyModelMixin,UpdateModelMixin,ListModelMixin,RetrieveModelMixin
# Create your views here.
# GenericAPIView 增删改查
class BookGenericAPIView(GenericAPIView,DestroyModelMixin,UpdateModelMixin,RetrieveModelMixin):
'''
提供序列化类
提供查询级
'''
serializer_class = BookSerializers
queryset = Book.objects.all()
def delete(self,request,pk):
'''
添加
:param request:
:return:
'''
return self.destroy(request)
def put(self,request,pk):
'''
添加
:param request:
:return:
'''
return self.update(request)
def get(self,request,pk):
'''
查询单个
:param request:
:return:
'''
return self.retrieve(request)
**********************************************************************************
class BookGenericAPIView1(GenericAPIView,CreateModelMixin,ListModelMixin):
'''
提供序列化类
提供查询级
'''
serializer_class = BookSerializers
queryset = Book.objects.all()
def post(self,request):
'''
添加
:param request:
:return:
'''
return self.create(request)
def get(self,request):
'''
查询全部
:param request:
:return:
'''
return self.list(request)
path('book1/',BookGenericAPIView1.as_view()),
path('book/<int:pk>/',BookGenericAPIView.as_view()),
from rest_framework.viewsets import ModelViewSet
class Mu(ModelViewSet):
serializer_class = UserSerializers
queryset = User.objects.all()
path('mu/',Mu.as_view({'get':'list','post':'create'})), #展示全部 创建
path('mu/<int:pk>/',Mu.as_view({'get':'retrieve','put':'update','delete':'destroy'})) #展示单个 修改 删除
抛出函数
export function get_all(url, params) {
return new Promise((resolve, reject) => {
axios({
'url': url,
'params': params,
}).then(resp => {
resolve(resp.data)
}).catch(error => {
reject(error)
})
}
)
}
接一下这个函数
import { get_all } from "../../http/index";
使用
get_all("book1/")
.then(resp => {
console.log(resp);
})
.catch(error => {
console.log(error);
});