Skip to content

drf-partial-response-fields

GraphQL-like sparse fieldsets for Django REST Framework, with automatic query optimization so that requesting fewer fields also means fewer database queries and less data read off the wire.

GET /api/articles/1/?fields=title,author(name,email),tags(label)

Why this exists

Most "sparse fieldset" packages for DRF stop at filtering the serializer's output. They shrink the JSON payload but leave the ORM query untouched — you still pay for every select_related-worthy join and every SerializerMethodField computation, even for fields the client never asked for. drf-partial-response-fields does both halves of the job:

  1. Output filtering — only the requested fields (and their requested nested sub-fields, at any depth) appear in the response.
  2. Query optimizationselect_related, prefetch_related, and only() are derived automatically from what was requested, including recursively-optimized nested Prefetch querysets.

See Performance for measured query-count reductions.

Where to go next

At a glance

Task Where
Filter fields on a serializer PartialFieldsSerializerMixin
Wire up a view/viewset PartialResponseMixin
Optimize a SerializerMethodField requires_related
Document ?fields= in OpenAPI PartialResponseAutoSchema
Change the query parameter name, strictness, depth limit, etc. Settings