def post(self, request): # 1、获取前端数据 data = request.data # 2、验证数据 初始化序列化器对象,将前端需要验证的数据传递给data参数 ser = BookSerializer(data=data) # 2-2 执行验证方法 ser.is_valid() # 2-3 获取验证后的状态信息 if ser.errors: return JsonResponse({'errors': ser.errors}) # 3、保存数据 # 调用序列器中的create方法完成保存 ser.save() # 4、返回结果 return Response(ser.data)
def post(self, request): serializer = BookSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response({'error': serializer.errors}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def book_detail_by_language(request, lang): """ Retrieve, update or delete a book. """ print "hey" try: # Filters books book = Book.objects.filter(language=lang) except Book.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = BookSerializer(book, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = BookSerializer(book, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': book.delete() return HttpResponse(status=204)
def post(self, request): # 1、获取前端数据 data = request.body.decode() data_dict = json.loads(data) # 2、验证数据 初始化序列化器对象,将前端需要验证的数据传递给data参数 ser = BookSerializer(data=data_dict) # 2-2 执行验证方法 ser.is_valid() # 2-3 获取验证后的状态信息 if ser.errors: return JsonResponse({'errors': ser.errors}) # 2-4 获取验证成功后的数据 validated_data = ser.validated_data # 3、保存数据 # 调用序列器中的create方法完成保存 ser.save() # 4、返回结果 return JsonResponse(ser.data)
def lend_retrieve(request, pk): try: book = Book.objects.get(pk=pk) except Book.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) serializer = BookSerializer(book, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response(serializer.data) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk): # 1、获取更新图书 try: book = BookInfo.objects.get(id=pk) except: return JsonResponse({"error": "图书不存在"}) # 2、获取更新字段数据 data = request.data # 3、验证数据 ser = BookSerializer(book, data=data) # 3-2 执行验证方法 try: ser.is_valid(raise_exception=True) except Exception as e: return Response({'errors': e.detail}) # 3-3 获取验证后的状态信息 # if ser.errors: # return JsonResponse({'errors': ser.errors}) # 4、更新数据 ser.save() # 5、返回结果 return Response(ser.data)
def put(self, request, pk): # 1、获取更新图书 try: book = BookInfo.objects.get(id=pk) except: return Response({"error": "图书不存在"}) # 2、获取更新字段数据 data = request.data # 3、验证数据 # 4、更新数据 book_update = BookSerializer(book, data=data) try: book_update.is_valid(raise_exception=True) except: return Response({'errors': book_update.errors}) # 1.抛出异常的方法 # if book_update.errors: # return JsonResponse({'errors':book_update.errors}) # 重新获取更新后的数据 book_update.save() # 5、返回结果 return Response(book_update.data)
def create(self, request, *args, **kwargs): try: serializer = BookSerializer(data=request.data) if serializer.is_valid(): serializer.validated_data['owner'] = Member.objects.get( id=request.data['owner']['id']) book = serializer.save() # book.save() return Response(BookSerializer(book).data, status=status.HTTP_201_CREATED) except Exception as ex: pass return Response(status=status.HTTP_400_BAD_REQUEST)
def book_list(request): """ List all books, or create a new book. """ if request.method == 'GET': books = Book.objects.all() serializer = BookSerializer(books, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = BookSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def create_book(request): data = JSONParser().parse(request) # convert keys from camelCase to snake_case data = converts_keys(data, case='snake') serializer = BookSerializer(data=data) if serializer.is_valid(): category, publisher, author = None, None, None category_id = data.get("category_id", None) publisher_id = data.get("publisher_id", None) author_id = data.get("author_id", None) if category_id: category = Category.objects.get(pk=category_id) if publisher_id: publisher = Publisher.objects.get(pk=publisher_id) if author_id: author = Author.objects.get(pk=author_id) serializer.save( category=category, publisher=publisher, author=author, ) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def book_detail_by_title(request, title): """ Retrieve, update or delete a book. """ try: book = Book.objects.get(title=title) except Book.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = BookSerializer(book) return JsonResponse(serializer.data) data = JSONParser().parse(request) elif request.method == 'PUT': serializer = BookSerializer(book, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': book.delete() return HttpResponse(status=204)