Member-only story
Your Django REST Framework Serializer Is Making 200 Database Queries Per Request
The queries aren’t in your view. They’re in your serializer.
We had an API endpoint that returned a list of 25 orders. The view was clean — one queryset, one serializer, paginated. In development, it responded in 180 milliseconds. In production, with real data and relationships, it took 4.7 seconds.
I opened django-debug-toolbar on the browsable API. 214 queries. For 25 orders.
If you’re not a Medium member, no worries! You can read the full story for free right here via : Friend Link
The view had select_related('customer'). The obvious N+1 was covered. But the serializer had a SerializerMethodField that accessed order.items.all(). Another nested serializer pulled item.product.category. A StringRelatedField on the product called __str__(), which accessed product.brand.name. Every one of these was a lazy load. Every lazy load fired a query. For every row.
25 orders × 4 items each × 2 lazy lookups per item = 200+ queries, generated entirely inside the serializer — invisible from the view.
The serializer wasn’t slow. The serializer was making the ORM do work the view never…











