36. Class-based views in the Django REST framework
We’ve already seen that provides the means of writing either function-based or class-based views.
The class based views gave us the opportunity to write much more streamlined code. And the Django REST Framework provides much the same abilities.
So instead of using Python functions with the various decorators, and simulating switch case statements, we can instead just use the REST framework class-based views.
The REST framework provides three ways of handling class-based views. The simplest uses the Rest framework APIView, and requires that we write our GET, POST, PUT, DELETE functions. And that’s much like the function based views that we’ve already seen.
Or if we want to customise the approach, we can use a mixins approach where we can tailor which of our views responds to which HTTP methods.
So let’s scroll up to the top of api.py, and we’re going to import a couple of packages:
from rest_framework import generics from rest_framework import mixins
Let’s scroll down and change the Genes List view. So we can add this code at the bottom:
class GeneList(generics.ListAPIView): queryset = Gene.objects.all() serializer_class = GeneListSerializer
This will function like the code we previously wrote:
@api_view(['GET']) def genes_list(request): if request.method == 'GET': gene = Gene.objects.all() serializer = GeneListSerializer(gene, many=True) return Response(serializer.data)
Of course we now have to jump back to urls.py and correct the view:
path('api/genes/', api.GeneList.as_view()),
Now we can turn our attention to the gene_detail function in api.py. We can comment out or remove the old function, and use a new function.
We want this to be able to create, restore, update and destroy, so we can inherit these functionalities from the mixins. We also inherit from the main generics class to give generic operations.
We need to define a queryset, and a serializer_class. Then we have to implement each of the functions that we want.
class GeneDetail(mixins.CreateModelMixin, mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, generics.GenericAPIView): queryset = Gene.objects.all() serializer_class = GeneSerializer def post(self, request, *args, **kwargs): return self.create(request, *args, **kwargs) def get(self, request, *args, **kwargs): return self.retrieve(request, *args, **kwargs) def put(self, request, *args, **kwargs): return self.update(request, *args, **kwargs) def delete(self, request, *args, **kwargs): return self.destroy(request, *args, **kwargs)
We need to change the path once again in urlpatterns in urls.py too:
path('api/gene/<int:pk>/', api.GeneDetails.as_view()),
This will now show the two API views – individual genes in GeneDetail and the whole list in GeneList – as it did before.
Wednesday 24 November 2021, 573 views
Next post: 37. Testing in Django Previous post: 35. Building a RESTful web service in Django
Advanced Web Development index
- 38. Writing API tests
- 37. Testing in Django
- 36. Class-based views in the Django REST framework
- 35. Building a RESTful web service in Django
- 34. Introduction to CRUD, REST and APIs
- 33. Refactoring with generic views in Django
- 32. Django validators
- 31. Django forms (2) – using the ModelForm class
- 30. Django forms (1)
- 29. JavaScript basics
- 28. Adding CSS to the template
- 27. Django templating
- 26. Deleting and updating records
- 25. Joins, filters and chaining commands
- 24. Using the ORM in views.py
- 23. Adding to the database by writing a script
- 22. Adding to the database with Django Admin
- 21. Migrations
- 20. ORM – work through example
- 19. An introduction to the Object-Relational Mapper
- 18. Altering the database
- 17. SQL functions and summaries
- 16. SQL Query performance
- 15. Queries and table joins in SQL
- 14. Inserts and queries in SQL
- 13. Good practice in relational database design
- 12. Limitations to database modelling
- 11. Building a database using SQL
- 10. Introduction to PostgreSQL
- 9. How to start writing a new application in Django
- 8. Building a lightweight project
- 7. Django URLs
- 6. Django templates
- 5. Django models
- 4. Django views
- 3. Creating a new hello app
- 2. Creating a new virtual environment
- 1. Setting up Django
Leave a Reply