def put(self, request, pk): todo = self.get_object(pk) serializer = TodoSerializer(todo, 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): serializer = TodoSerializer(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 todo_list(request): if request.method == 'GET': todo = Todo.objects.all() serializer = TodoSerializer(todo, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = TodoSerializer(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 todo_detail_change_and_delete(request, pk): try: todo = Todo.objects.get(pk=pk) except Todo.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = TodoSerializer(todo) return Response(serializer.data) elif request.method == 'PUT': serializer = TodoSerializer(todo, data = request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': todo.delete() return Response(status= status.HTTP_204_NO_CONTENT)
def get(self, request, pk): todo = self.get_object(pk) serializer = TodoSerializer(todo) return Response(serializer.data)
def get(self, request): todo = Todo.objects.all() serializer = TodoSerializer(todo, many=True) return Response(serializer.data)
def get(self, request, pk): serializer = TodoSerializer(todo) return Response(serializer.data)
from app.models import Todo from app.serializers import TodoSerializer todo = Todo.objects.first() serializer = TodoSerializer(todo) serializer.data