-
Don't forget define the
related_name
forManyToManyField
. -
Don't forget return
Response
witherrors
if serializer is not valid:
Good example:
class GenreList(APIView):
def post(self, request):
serializer = GenreSerializer(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)
Another good example:
class GenreList(APIView):
def post(self, request):
serializer = GenreSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
Bad example:
class GenreList(APIView):
def post(self, request):
serializer = GenreSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
Another bad example:
class GenreList(APIView):
def post(self, request):
serializer = GenreSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.data, status=status.HTTP_400_BAD_REQUEST)
- Group imports using
()
if needed.
Good example:
from django.contrib.auth.mixins import (
LoginRequiredMixin,
UserPassesTestMixin,
PermissionRequiredMixin,
)
Bad example:
from django.contrib.auth.mixins import LoginRequiredMixin, \
UserPassesTestMixin, PermissionRequiredMixin
Another bad example:
from django.contrib.auth.mixins import (
LoginRequiredMixin,
UserPassesTestMixin, PermissionRequiredMixin,
)
Add comments, prints, and functions to check your solution when you write your code. Don't forget to delete them when you are ready to commit and push your code.