def put(self, request, pk, format=None): category = Category.objects.get(pk=pk) serializer = CategorySerializer(category, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk): category = self.get_object(pk) serializer = CategorySerializer(category, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): data = request.data serializer = CategorySerializer(data=data) if serializer.is_valid(): serializer.save() response = generateresponse('Success', 'Category', serializer.data) return Response(response) else: raise InvalidInformation(serializer.errors)
def test_category_serializer(self): data = {'name': 'food'} category_serializer = CategorySerializer(data=data) self.assertTrue(category_serializer.is_valid()) category = category_serializer.save() self.assertEqual(category.name, data['name'])
def category_list(request): """ :GET get all main categories :POST add new category :param request: :return: Response obj """ if request.method == 'GET': categories = Categories.objects.all() serializer = CategorySerializer(categories, many=True) return Response(serializer.data, status=status.HTTP_200_OK) if request.method == 'POST' and 'user_name' in request.data.keys( ) and 'data' in request.data.keys(): user = Users.objects.get(user_name=request.data['user_name']) data = request.data['data'] if user and 'category_name' in data.keys(): serializer = CategorySerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(EMPTY_DICT, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None): serializer = CategorySerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): serialized = CategorySerializer(data=request.data) if serialized.is_valid(): serialized.save() return Response(serialized.data, status=status.HTTP_201_CREATED) return Response(serialized.errors, status=status.HTTP_400_BAD_REQUEST)