Display Multiple Models in One Django Rest Framework View with DjangoRestMultipleModels
Django REST Framework is a powerful package that makes it easy to create REST API's for Django. It comes bundled with everything you need to create a complete API quickly.
There are some edge-cases however, for example when you want to display multiple models in view it becomes tricky and cumbersome. A fix is not far of though.
The package DjangoRestMultipleModels makes it really easy to add multiple models in one view without writing a custom view by your self.
Let's Install DjangoRestMultipleModels
pip install django-rest-multiple-models
Make sure to add ‘drfmultiplemodel’ to your INSTALLED_APPS:
#settings.py
INSTALLED_APPS = (
....
'drf_multiple_model',
)
Let's Create Our View with Multiple Models
In this example we are building a hompage where we want to list out two different models to a single view so that it can easily be absorbed by our frontend through one URL endpoint.
You will need to have an ArticleSerializer and a CommentSerializer ready made because we want to display a list of the 5 latest Articles and Comments.
#views.py
from drf_multiple_model.views import ObjectMultipleModelAPIView
class HomeAPIView(ObjectMultipleModelAPIView):
permission_classes = (AllowAny,)
pagination_class = None
querylist = [
{'queryset': Article.objects.order_by('-created_at')[:5], 'serializer_class': ArticleSerializer},
{'queryset': Comment.objects.order_by('-created_at')[:5], 'serializer_class': CommentSerializer},
]
As you can see we load multiple querysets with in the view with: querylist = [ ]
Make sure to specify not to use pagination pagination_class = None . As DjangoRestMultipleModels does not support the regular way Django Rest Framework does pagination.
#terminal
NotImplementedError: HomeAPIView cannot use the regular Rest Framework or Django paginators as is. Use one of the included paginators from `drf_multiple_models.pagination or subclass a paginator to add the `format_response` method.
In our use-case we did not need pagination, however, if you really need pagination DjangoRestMultipleModels provides a way to do Limit/OffSet pagination.
#views.py
from drf_multiple_model.views import ObjectMultipleModelAPIView
from drf_multiple_model.pagination import MultipleModelLimitOffsetPagination
class LimitPagination(MultipleModelLimitOffsetPagination):
default_limit = 2
class ObjectLimitPaginationView(ObjectMultipleModelAPIView):
pagination_class = LimitPagination
querylist = (
{'queryset': Article.objects.all(), 'serializer_class': ArticleSerializer},
{'queryset': Comment.objects.all(), 'serializer_class': CommentSerializer},
)
As you can see for yourself, DjangoRestMultipleModels makes it super simple to combine many models into one big queryset without writing your own view that combines queryset manually.
This tutorial is just scraping the surface of the package and its capabilities, to get a better understanding of the DjangoRestMultipleModels package I encourage you to consolidate the documentation further.
I hope you enjoyed the tutorial and if you have any questions or inputs, just let me know in the comments below.