Performance: Nested serializers
MEDIUM IMPACT
Nested serializers affect the server response time and the size of JSON payloads, impacting page load speed and interaction responsiveness.
class AuthorSerializer(serializers.ModelSerializer): books = serializers.PrimaryKeyRelatedField(many=True, read_only=True) class Meta: model = Author fields = ['id', 'name', 'books'] # Only book IDs are serialized, reducing payload size and processing.
class AuthorSerializer(serializers.ModelSerializer): books = BookSerializer(many=True) class Meta: model = Author fields = ['id', 'name', 'books'] # This loads all book details nested inside each author, even if not needed.
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Deep nested serializers with full objects | N/A (server-side) | N/A | High due to large JSON parsing | [X] Bad |
| Flat serializers with related IDs only | N/A (server-side) | N/A | Low JSON parsing cost | [OK] Good |