🧩 Serializers
Each exposed model is serialized with a ModelSerializer that includes all fields, minus those left out with fields or exclude (see Exposing models). To expand relations and serialize model properties, switch to FlexibleSerializer:
# mysite/settings.py
HEADLESS = {
"DEFAULT_SERIALIZER_CLASS": "headless.rest.serializers.FlexibleSerializer",
}
HEADLESS["DEFAULT_SERIALIZER_CLASS"] sets the base class for the serializers of all exposed models.
FlexibleSerializer
FlexibleSerializer builds on drf-flex-fields and adds two things to the default serializer:
- Model properties are serialized as read-only fields. Properties whose name starts with
_are left out. - Relations to other exposed models can be expanded inline with the
expandquery parameter.
# blog/models.py
from django.db import models
from headless import expose
@expose()
class Category(models.Model):
name = models.CharField(max_length=100)
@expose()
class Article(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
category = models.ForeignKey(Category, on_delete=models.PROTECT, related_name="articles")
@property
def word_count(self):
return len(self.body.split())
Every article now includes word_count.
Expanding relations
By default, a relation is serialized as the primary key of the related object. Name the relation in expand to include the related object instead:
GET /api/blog.article/1?expand=category
GET /api/blog.category/1?expand=articles
{
"id": 1,
"title": "Getting started with Django",
"category": {
"id": 2,
"name": "Tutorials"
},
"word_count": 1250,
"...": "..."
}
Forward relations are expanded by field name (category). Reverse relations are expanded by the name they have on an object: their related_name (articles), or <model>_set without one, such as review_set for the reviews of an article. Only relations to exposed models can be expanded; other relations stay primary keys.
An expanded object is serialized with HEADLESS["DEFAULT_SERIALIZER_CLASS"], so it expands further only when that is FlexibleSerializer. Paths are limited to two relations, such as ?expand=category.articles, and a longer path answers 400 Bad Request. Change the limit with HEADLESS["MAX_EXPANSION_DEPTH"].
An expanded object follows the fields and exclude options of its own model. Relations and properties left out with fields or exclude are not serialized and cannot be expanded.
Queries
The number of queries of a route does not grow with the number of objects in a response. An expanded foreign key or one-to-one relation is loaded together with the object, and every other expanded relation takes one query. Many-to-many fields (and reverse relations that are serialized) take one query each as well, whether or not they are expanded, because every object in a list lists the primary keys of its related objects.
Relations that ?fields= or ?omit= leave out of the response are not loaded, so ?fields=title,slug takes no queries for relations at all.
A model property that queries the database runs once per object. Keep such properties cheap, or leave them out of the API with exclude.