Routers gave us Purchase as a full ViewSet - list, retrieve, create, update, destroy, all wired up. Somewhere alongside it there’s a second model, PurchasedItem, the actual line items of a purchase (product_name, quantity, unit_price), tied back with a plain foreign key:
# purchases/models.py
class PurchasedItem(models.Model):
purchase = models.ForeignKey(Purchase, on_delete=models.CASCADE, related_name="items")
product_name = models.CharField(max_length=255, blank=True, null=True)
quantity = models.PositiveIntegerField(default=1)
unit_price = models.DecimalField(max_digits=10, decimal_places=2)
PurchasedItem only has a detail endpoint - purchased-items/<int:pk>/ - no list route at all. That’s not an oversight. A flat table of “every purchased item across every purchase, unrelated to which purchase they belong to” isn’t a page anyone asks for. You don’t browse “all order line-items ever” on a real site - you open an order and see its items. So the actual question isn’t “where’s the list endpoint for PurchasedItem” - it’s “how does opening one Purchase show me its items.”
Where ‘all’ quietly stops
The instinct is: Purchase already has a retrieve endpoint, and PurchasedItem has that related_name=“items” sitting right there on the foreign key. Surely PurchaseSerializer already exposes it?
class PurchaseSerializer(serializers.ModelSerializer):
class Meta:
model = Purchase
fields = '__all__'
It doesn’t. ’all’ expands to Purchase’s own concrete columns - id, whatever fields live directly on the model - and nothing else. A related_name is a reverse relation, not a field on the model itself, so ModelSerializer never picks it up no matter what you put in Meta.fields. Hit GET /api/purchases/5/ and the items are simply not there. Not hidden, not filtered out - never included in the first place.
Declaring the nested field
The fix is one line, but it’s worth being precise about what each part of it is doing:
class PurchaseSerializer(serializers.ModelSerializer):
items = PurchasedItemSerializer(many=True, read_only=True)
class Meta:
model = Purchase
fields = '__all__'
items = PurchasedItemSerializer(…) is a serializer used as a field on another serializer - that’s the whole trick behind “nested.” Instead of a plain CharField or IntegerField, the value at this key is itself the output of running every related object through PurchasedItemSerializer.
many=True because one purchase can have several items - without it, DRF would expect exactly one PurchasedItem instance here, not a list, and blow up the moment there’s more than one.
read_only=True because editing an item should go through PurchasedItemDetailView - the endpoint that already exists for exactly that - not through updating a Purchase. Worth being precise about what that view actually covers, though: it’s a RetrieveUpdateAPIView - GET and PUT/PATCH, no POST. There’s no creation endpoint for PurchasedItem at all right now; a new item only ever shows up as part of its parent Purchase. A serializer field being present doesn’t mean writable; declaring it explicitly is how you say “show this, but this isn’t the door for changing it.”
One easy trap: PurchasedItemSerializer has to be defined above PurchaseSerializer in the file. Python reads top to bottom - by the time PurchaseSerializer references PurchasedItemSerializer as a field, that name has to already exist.
What actually comes back now
Same endpoint as before, same ViewSet, same router wiring - GET /api/purchases/5/ now returns:
{
"id": 5,
"price": "34.48",
"items": [
{ "id": 12, "purchase": 5, "product_name": "Widget", "quantity": 2, "unit_price": "9.99" },
{ "id": 13, "purchase": 5, "product_name": "Gadget", "quantity": 1, "unit_price": "24.50" }
],
...
}
No new view, no new URL, no separate request for the items. The one line on the serializer is the entire feature.
Where this leaves us
Worth being honest about what this isn’t: this is a read-only nested serializer. It’s genuinely good at showing a one-to-many relationship in one response, and that’s a common enough need that it’s worth knowing on its own. It is not the same problem as accepting a nested payload on POST and creating both the Purchase and its items in one write - that needs a custom create()/update() on the serializer to manually pull the nested data back apart, and DRF doesn’t do that for you automatically the way it does for reads. That’s a sharper, harder version of this same idea, worth its own look once the read side is second nature.