Created
April 24, 2020 10:07
-
-
Save paulonteri/02e9f3eab3d25170e888037dbc935430 to your computer and use it in GitHub Desktop.
A simple APIView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import status # provides identifiers for each status code | |
from rest_framework.response import Response # determines the correct content type to return | |
from rest_framework.views import APIView | |
from .models import Note | |
from .serializers import NoteSerializer | |
class NoteApi(APIView): | |
# list or add Note instances | |
# List | |
def get(self, request, format=None): | |
notes = Note.objects.all() | |
serializer = NoteSerializer(notes, many=True) | |
return Response(serializer.data) | |
# Add | |
def post(self, request, format=None): | |
serializer = NoteSerializer(data=request.data) | |
# verify that the data passed to the Serializer is valid | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment