def set_bookname(self, request, pk=None): book = self.get_object() serializer = BookModelSerializer(data=request.data) if serializer.is_valid(): book.name = request.data['name'] book.save() return Response({'message': '重置成功'}) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request): """ 1.获取模型列表 2.我们创建序列化器,让序列化器对我们的模型列表转换 3.返回响应 """ # 1.获取模型列表 books = BookInfo.objects.all() # 2.我们创建序列化器,让序列化器对我们的模型列表转换 serializer = BookModelSerializer(books, many=True) # 3.返回响应 return Response(serializer.data)
def get(self, request, *args, **kwargs): book_obj = Book.objects.all() book_list = BookModelSerializer(instance=book_obj, many=True).data tag_obj = Tags.objects.all() tags_list = TagModelSerializer(instance=tag_obj, many=True).data author_obj = Author.objects.all()[4:8] author_list = AuthorModelSerializer(instance=author_obj, many=True).data category_obj = Category.objects.all() category_list = CategoryModelSerializer(instance=category_obj, many=True).data # 轮播图单个页面渲染 i = book_list[:3] m = book_list[3:6] n = book_list[6:9] p = book_list[9:12] q = book_list[12:15] # 前端标签渲染 d = book_list[11:15] x = book_list[15:19] return render(request, 'home.html', locals())
def post(self, request): """ # 1.接收数据 # 2.校验数据 # 3.创建对象,保存到数据库 # 4.返回响应 """ # 1.接收数据 data = request.data # 2.校验数据,我们用序列化器 serializer = BookModelSerializer(data=data) serializer.is_valid(raise_exception=True) # 3.创建对象,保存到数据库 serializer.save() # 4.返回响应 return Response(serializer.data)
def get(self, request): books = BookInfo.objects.all().order_by('readcount') serializer = BookModelSerializer(books, many=True) return Response(serializer.data)
def retrieve(self, request, pk=None): #GET queryset = BookInfo.objects.all() user = get_object_or_404(queryset, pk=pk) serializer = BookModelSerializer(user) return Response(serializer.data)
def list(self, request): #GET queryset = BookInfo.objects.all() serializer = BookModelSerializer(queryset, many=True) return Response(serializer.data)