def put(self, request): queryset = Question.objects.get(id = request.data['id']) serializer = QuestionSerializer(queryset, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_404_NOT_FOUND)
def post(self, request): serializer = QuestionSerializer(data=request.data) try: if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) except Exception as e: print(e) return Response(serializer.errors, status=status.HTTP_404_NOT_FOUND)
def question_list_or_post(request): """List all Questions or create a new one.""" if request.method == 'GET': questions = Question.objects.all() serializer = QuestionSerializer(questions, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = QuestionSerializer(data=request.data) if serializer.is_valid(): serializer.save(user=Profile.objects.get(user=request.user)) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def question_get_edit_delete(request, pk): """Get, Edit or Delete specific Question if you are an owner.""" try: question = Question.objects.get(pk=pk) except Question.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = QuestionSerializer(question) return Response(serializer.data) if request.method == 'PUT': serializer = QuestionSerializer(question, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) if request.method == 'DELETE': question.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def post(self, request, format=None): serializer = QuestionSerializer(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)