
Sometimes you can’t express the additional field you need this way.

The great thing about adding fields with queryset annotations is that it’s also efficient and prevents using an excessive number of SQL queries. Here I’m adding positions_count and transactions_count :Ĭlass AccountSerializer(serializers.
CUSTOM SERIALIZER DJANGO REST FRAMEWORK UPDATE


private data visible only to the owning user.My app supports multiple users and they each have their own private data. Only show entities related to the current user I will provide a bit of a context, general motivation for using a given recipe and an example. They relate to a similar class of problems. The recipes are sorted from most common to more complex and are grouped together when If you want to know more about setting up typechecking for django rest framework I recommend this article. delete endpoint that in this case is very standardĮxamples are using python type hints and are coming from a project using python 3.8 (at the time of writing).create and update endpoints that only allow to set or touch limited number of parameters.very rich detail get endpoint ‘/accounts/id/’ for retrieving detailed account data that takes additional parameters (from_date, to_date).list endpoint ‘/accounts/’ that lists the accounts based on the model and provides a bunch of additional fields within each model.I’m using drf ViewSet to provide the following: The api is consumed by a react frontend and it’s customized by the need of the frontend app.

I’ve been working on a feature in the invertimo app where users can add multiple different investment accounts. This article doesn’t provide any introduction to django or the django rest framework. They all come from just a single ViewSet (set of related api endpoints)!Īt the end of the article I will show how all those bits fit together. Recipes from my use of django rest framework taken from my latest project. In this article I’m going to share a bunch of The documentation of django rest framework is pretty extensive, there is a great tutorial, api docs and plenty of examples.īut reality is usually a bit more complex and you need to customize your use of the framework. It’s easy to set up, extensible and saves so much time. One of my favorite tools in my app development toolkit is django rest framework (drf), that makes developing REST APIs with python and django easy and fun.
