def get(self, request, *args, **kwargs): book_id = kwargs.get('id') if book_id: book = Book.objects.get(pk=book_id, is_delete=False) book_data = BookModelSerializer(book).data return Response({ 'status': 200, 'message': '查询单本图书成功', 'result': book_data }) books = Book.objects.filter(is_delete=False) books_data = BookModelSerializer(books, many=True).data return Response({ 'status': 200, 'message': '查询所有图书成功', 'result': books_data })
def get(self, requset, *args, **kwargs): book_id = kwargs.get('id') if book_id: book = Book.objects.get(pk=book_id, is_delete=False) data = BookModelSerializer(book).data return Response({ 'status': 200, 'message': '查询单个图书成功', 'result': data, }) else: book_object_all = Book.objects.filter(is_delete=False) book_ser = BookModelSerializer(book_object_all, many=True).data return Response({ 'status': 200, 'message': '查询所有图书成功', 'result': book_ser, })
def get(self, request, *args, **kwargs): id = kwargs.get("id") if id: try: book = Book.objects.get(id=id, is_delete=False) data = BookModelSerializer(book).data return Response({ "status": 200, "messages": "查询单个", 'result': data }) except Book.DoesNotExist: return Response({"status": 200, "messages": "该书不存在"}) else: book_all = Book.objects.all() book_all_ser = BookModelSerializer(book_all, many=True).data return Response({ "status": 200, "messages": "查询全部", 'result': book_all_ser })
def user_login(self,request,*args,**kwargs): request_data = request.data username = request_data.get("username") password = request_data.get("password") res = User.objects.filter(username=username,password=password) serializer = BookModelSerializer(request_data).data if res: return Response({ "status":200, "message":"登录成功", "results":serializer, }) return Response({ "status":400, "message":"登录失败", })
def put(self, request, *args, **kwargs): book_id = kwargs.get("id") request_data = request.data try: book = Book.objects.get(pk=book_id) except Book.DoesNotExist: return Response({'status': 400, 'message': '图书不存在'}) serializer = BookModelSerializer(data=request_data, instance=book) serializer.is_valid(raise_exception=True) serializer.save() return Response({ 'status': 200, 'message': '修改成功', 'result': BookModelSerializer(book).data })
def put(self, request, *args, **kwargs): data = request.data id = kwargs.get("id") try: book = Book.objects.get(id=id) except Book.DoesNotExist: return Response({"status": 400, "messages": "图书不存在"}) serializer = BookModelSerializer(data=data, instance=book) serializer.is_valid(raise_exception=True) serializer.save() return Response({ "status": 200, "messages": "修改单个成功", 'result': BookModelSerializer(book).data })
def put(self, request, *args, **kwargs): request_data = request.data book_id = kwargs.get('id') try: book_obj = Book.objects.get(pk=book_id) except Book.DoesNotExist: return Response({"status": 400, "message": '图书不存在'}) serializer = BookModelSerializer(data=request_data, instance=book_obj) serializer.is_valid(raise_exception=True) serializer.save() return Response({ "status": 200, "message": '修改成功', "results": BookModelSerializer(book_obj).data })
def patch(self, request, *args, **kwargs): data = request.data id = kwargs.get('id') print("data", data) print("id", id) if id and isinstance(data, dict): ids = [id] request_data = [data] elif not id and isinstance(data, list): # 修改多个 ids = [] for dic in data: pk = dic.pop("id", None) print("pk", pk) if pk: ids.append(pk) else: return Response({ "status": status.HTTP_400_BAD_REQUEST, "message": "PK不存在", }) else: return Response({ "status": status.HTTP_400_BAD_REQUEST, "message": "参数格式有误", }) book_list = [] new_data = [] for index, pk in enumerate(ids): try: book_obj = Book.objects.get(pk=pk) book_list.append(book_obj) new_data.append(request.data[index]) except Book.DoesNotExist: continue print('newdata', new_data) print('booklist', book_list) book_ser = BookModelSerializer(data=new_data, instance=book_list, partial=True, many=True) book_ser.is_valid(raise_exception=True) book_ser.save() return Response({ "status": status.HTTP_200_OK, "message": "修改成功", })
def post(self, request, *args, **kwargs): request_data = request.data if isinstance(request_data, dict): flag = False elif isinstance(request_data, list): flag = True else: return Response({'status': 400, 'message': '参数有误,添加失败'}) serializer = BookModelSerializer(data=request_data, many=flag) serializer.is_valid(raise_exception=True) books = serializer.save() return Response({ 'status': 200, 'message': '图书添加成功', 'result': BookModelSerializer(books, many=flag).data })
def post(self, request, *args, **kwargs): data = request.data print("datadata", data) if isinstance(data, dict): many = False elif isinstance(data, list): many = True else: return Response({ "status": 400, "messages": "参数错误", }) serializer = BookModelSerializer(data=data, many=many) serializer.is_valid(raise_exception=True) book_obj = serializer.save() return Response({ "status": 200, "messages": "添加成功", 'result': BookModelSerializer(book_obj, many=many).data })
def user_register(self,request,*args,**kwargs): request_data = request.data username = request_data.get("username") password = request_data.get("password") # print(username) # print(password) # print(request_data) res = User.objects.filter(username=username) if res: return Response({ "status":400, "message":"用户名已存在", }) serializer = BookModelSerializer(data=request_data) serializer.is_valid(raise_exception=True) user_obj = serializer.save() return Response({ "status":200, "message":"注册成功", "results":BookModelSerializer(user_obj).data, })
def post(self, request, *args, **kwargs): request_data = request.data if isinstance(request_data, dict): flag = False elif isinstance(request_data, list): flag = True else: return Response({ "status": 400, "message": "参数格式有误", }) serializer = BookModelSerializer(data=request_data, many=flag) serializer.is_valid(raise_exception=True) book_obj = serializer.save() return Response({ "status": 200, "message": "添加图书成功", "results": BookModelSerializer(book_obj, many=flag).data, })