This commit is contained in:
AvalancheTheory 2016-12-01 09:30:50 +00:00 committed by GitHub
commit f61facba8d
4 changed files with 24 additions and 14 deletions

View File

@ -97,7 +97,7 @@ The first thing we need to get started on our Web API is to provide a way of ser
def create(self, validated_data): def create(self, validated_data):
""" """
Create and return a new `Snippet` instance, given the validated data. Create and return a new `Snippet` instance given the validated data.
""" """
return Snippet.objects.create(**validated_data) return Snippet.objects.create(**validated_data)
@ -239,7 +239,7 @@ The root of our API is going to be a view that supports listing all the existing
@csrf_exempt @csrf_exempt
def snippet_list(request): def snippet_list(request):
""" """
List all code snippets, or create a new snippet. List all code snippets or create a new snippet.
""" """
if request.method == 'GET': if request.method == 'GET':
snippets = Snippet.objects.all() snippets = Snippet.objects.all()
@ -252,7 +252,8 @@ The root of our API is going to be a view that supports listing all the existing
if serializer.is_valid(): if serializer.is_valid():
serializer.save() serializer.save()
return JSONResponse(serializer.data, status=201) return JSONResponse(serializer.data, status=201)
return JSONResponse(serializer.errors, status=400) else:
return JSONResponse(serializer.errors, status=400)
Note that because we want to be able to POST to this view from clients that won't have a CSRF token we need to mark the view as `csrf_exempt`. This isn't something that you'd normally want to do, and REST framework views actually use more sensible behavior than this, but it'll do for our purposes right now. Note that because we want to be able to POST to this view from clients that won't have a CSRF token we need to mark the view as `csrf_exempt`. This isn't something that you'd normally want to do, and REST framework views actually use more sensible behavior than this, but it'll do for our purposes right now.
@ -261,7 +262,7 @@ We'll also need a view which corresponds to an individual snippet, and can be us
@csrf_exempt @csrf_exempt
def snippet_detail(request, pk): def snippet_detail(request, pk):
""" """
Retrieve, update or delete a code snippet. Retrieve, update, or delete a code snippet.
""" """
try: try:
snippet = Snippet.objects.get(pk=pk) snippet = Snippet.objects.get(pk=pk)
@ -278,7 +279,8 @@ We'll also need a view which corresponds to an individual snippet, and can be us
if serializer.is_valid(): if serializer.is_valid():
serializer.save() serializer.save()
return JSONResponse(serializer.data) return JSONResponse(serializer.data)
return JSONResponse(serializer.errors, status=400) else:
return JSONResponse(serializer.errors, status=400)
elif request.method == 'DELETE': elif request.method == 'DELETE':
snippet.delete() snippet.delete()

View File

@ -47,7 +47,7 @@ We don't need our `JSONResponse` class in `views.py` anymore, so go ahead and de
@api_view(['GET', 'POST']) @api_view(['GET', 'POST'])
def snippet_list(request): def snippet_list(request):
""" """
List all snippets, or create a new snippet. List all snippets or create a new snippet.
""" """
if request.method == 'GET': if request.method == 'GET':
snippets = Snippet.objects.all() snippets = Snippet.objects.all()
@ -59,7 +59,8 @@ We don't need our `JSONResponse` class in `views.py` anymore, so go ahead and de
if serializer.is_valid(): if serializer.is_valid():
serializer.save() serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Our instance view is an improvement over the previous example. It's a little more concise, and the code now feels very similar to if we were working with the Forms API. We're also using named status codes, which makes the response meanings more obvious. Our instance view is an improvement over the previous example. It's a little more concise, and the code now feels very similar to if we were working with the Forms API. We're also using named status codes, which makes the response meanings more obvious.
@ -68,7 +69,7 @@ Here is the view for an individual snippet, in the `views.py` module.
@api_view(['GET', 'PUT', 'DELETE']) @api_view(['GET', 'PUT', 'DELETE'])
def snippet_detail(request, pk): def snippet_detail(request, pk):
""" """
Retrieve, update or delete a snippet instance. Retrieve, update, or delete a snippet instance.
""" """
try: try:
snippet = Snippet.objects.get(pk=pk) snippet = Snippet.objects.get(pk=pk)
@ -84,7 +85,8 @@ Here is the view for an individual snippet, in the `views.py` module.
if serializer.is_valid(): if serializer.is_valid():
serializer.save() serializer.save()
return Response(serializer.data) return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
elif request.method == 'DELETE': elif request.method == 'DELETE':
snippet.delete() snippet.delete()

View File

@ -16,8 +16,10 @@ We'll start by rewriting the root view as a class-based view. All this involves
class SnippetList(APIView): class SnippetList(APIView):
""" """
List all snippets, or create a new snippet. List all snippets or create a new snippet.
""" """
queryset = Snippet.objects.all()
def get(self, request, format=None): def get(self, request, format=None):
snippets = Snippet.objects.all() snippets = Snippet.objects.all()
serializer = SnippetSerializer(snippets, many=True) serializer = SnippetSerializer(snippets, many=True)
@ -28,14 +30,17 @@ We'll start by rewriting the root view as a class-based view. All this involves
if serializer.is_valid(): if serializer.is_valid():
serializer.save() serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
So far, so good. It looks pretty similar to the previous case, but we've got better separation between the different HTTP methods. We'll also need to update the instance view in `views.py`. So far, so good. It looks pretty similar to the previous case, but we've got better separation between the different HTTP methods. We'll also need to update the instance view in `views.py`.
class SnippetDetail(APIView): class SnippetDetail(APIView):
""" """
Retrieve, update or delete a snippet instance. Retrieve, update, or delete a snippet instance.
""" """
queryset = Snippet.objects.all()
def get_object(self, pk): def get_object(self, pk):
try: try:
return Snippet.objects.get(pk=pk) return Snippet.objects.get(pk=pk)
@ -53,7 +58,8 @@ So far, so good. It looks pretty similar to the previous case, but we've got be
if serializer.is_valid(): if serializer.is_valid():
serializer.save() serializer.save()
return Response(serializer.data) return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, pk, format=None): def delete(self, request, pk, format=None):
snippet = self.get_object(pk) snippet = self.get_object(pk)

View File

@ -30,7 +30,7 @@ Next we're going to replace the `SnippetList`, `SnippetDetail` and `SnippetHighl
class SnippetViewSet(viewsets.ModelViewSet): class SnippetViewSet(viewsets.ModelViewSet):
""" """
This viewset automatically provides `list`, `create`, `retrieve`, This viewset automatically provides `list`, `create`, `retrieve`,
`update` and `destroy` actions. `update`, and `destroy` actions.
Additionally we also provide an extra `highlight` action. Additionally we also provide an extra `highlight` action.
""" """