I see you define a GET route that needs extra logic (a parameter extraction and a filter) apart from your basic "display data" code, but for any view that doesn't need special functionality (like search), these routes would be created with only about three lines of code (below). If you define your model and serializer the same way as in the video, this is how views.py would look in the simplest of APIs using this framework:
class FoodViewSet(viewsets.ModelViewSet):
queryset = Food.objects.all()
serializer_class = FoodSerializer
For your example, instead of defining a get method as part of an APIView, we use the list action (one of six actions of a ViewSet) by defining the list method:
views.py
from rest_framework import viewsets
from rest_framework.response import Response
from .models import Food
from .serializers import FoodSerializer
class FoodViewSet(viewsets.ModelViewSet):
queryset = Food.objects.all()
serializer_class = FoodSerializer
def list(self, request):
query = self.request.GET.get('search')
food = self.queryset
if query is not None:
food = self.queryset.filter(name__contains=query) | \
self.queryset.filter(creator__contains=query)
serializer = FoodSerializer(food, many=True)
return Response(serializer.data)
Then your URLs might look like this:
urls.py
from django.urls import re_path, include
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'foods', views.FoodViewSet) # No trailing slash in regex
urlpatterns = router.urls
Now the views.py code looks very similar, but the main takeaway is that all of our views, URLs, and CRUD functionality is there with very little code, when in your video, you've only defined one view. Using ViewSets, we've written five more without any extra effort.
Imagine having to write every single GET, POST, PUT, PATCH, and DELETE method for an API that contains several models, repeating the same patterns, and then imagine having the same functionality without even having to write those views (in most cases). I recommend going through the complete documentation, as it's an incredibly useful framework.
That's very interesting! I'll definitely check it out, just started getting into the Django REST framework. It looks like what you've outlined is probably a better way of going about it than I did. I'll consider this for future projects! Thank you!
2
u/McCarthyCode Feb 12 '21 edited Feb 12 '21
You're using Django REST Framework, why not use ViewSets? ViewSets set up all your GET all, GET one, POST, PUT, PATCH, and DELETE views for you.
https://www.django-rest-framework.org/api-guide/viewsets/
I see you define a GET route that needs extra logic (a parameter extraction and a filter) apart from your basic "display data" code, but for any view that doesn't need special functionality (like search), these routes would be created with only about three lines of code (below). If you define your model and serializer the same way as in the video, this is how views.py would look in the simplest of APIs using this framework:
For your example, instead of defining a
getmethod as part of anAPIView, we use thelistaction (one of six actions of a ViewSet) by defining thelistmethod:views.py
Then your URLs might look like this:
urls.py
Now the views.py code looks very similar, but the main takeaway is that all of our views, URLs, and CRUD functionality is there with very little code, when in your video, you've only defined one view. Using ViewSets, we've written five more without any extra effort.
Imagine having to write every single GET, POST, PUT, PATCH, and DELETE method for an API that contains several models, repeating the same patterns, and then imagine having the same functionality without even having to write those views (in most cases). I recommend going through the complete documentation, as it's an incredibly useful framework.