def post(self,request): serializer=PurchaseSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data,status=status.HTTP_201_CREATED) else: return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)
def put(self,request,pk): purchases=self.get_object(pk) serializer=PurchaseSerializer(purchases,data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data,status=status.HTTP_200_OK) else: return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) try: purchase = self.perform_create(serializer) except (ErrorTPaga, ConnectionTPaga) as e: return Response({'error_message': e.__str__()}, status=status.HTTP_422_UNPROCESSABLE_ENTITY) serializer = PurchaseSerializer(purchase) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def perform_create(self, serializer): """Добавляет рецепт в список покупок""" recipe = get_object_or_404(Recipe, pk=self.request.data.get('id')) serializer = PurchaseSerializer(data=self.request.data) serializer.is_valid(raise_exception=True) serializer.save(user=self.request.user, recipe=recipe) return Response({'success': True})
def get(self,request,pk): purchases=self.get_object(pk) serializer=PurchaseSerializer(purchases) return Response(serializer.data)
def get(self,request): purchases=ps.objects.all() serializer=PurchaseSerializer(purchases,many=True) return Response(serializer.data)