Skip to main content

drf-partial-response-fields

PyPI version Python versions License: MIT

GraphQL-like sparse fieldsets for Django REST Framework — with automatic select_related / prefetch_related / only() query optimization, so requesting fewer fields also means fewer queries and less data transferred.

GET /api/articles/1/?fields=title,author(name,email),tags(label)
{
  "title": "Shipping Faster with Sparse Fieldsets",
  "author": { "name": "Ada Lovelace", "email": "ada@example.com" },
  "tags": [{ "label": "performance" }, { "label": "django" }]
}

Why

Fetching the full representation of a resource is wasteful when a client only needs a few fields — especially on mobile, in list views, or in BFF-style aggregation layers. Most "sparse fieldset" implementations for DRF only filter the serializer output and leave the ORM query untouched, so you save bytes on the wire but still pay for every join and every SerializerMethodField query. This package does both: it filters what gets serialized and rewrites the queryset so the database only does the work that's actually needed.

Features

  • Nested field selection: ?fields=title,author(name,email)
  • Deep nesting: any depth, e.g. author(company(name,address(city)))
  • Exclusion: ?fields=-internal_notes,-legacy_id
  • Aliasing: ?fields=publishedAt:created_at
  • Wildcards: ?fields=* (explicit "no restriction")
  • Works with: ModelSerializer, nested serializers, SerializerMethodField, pagination, ViewSet/GenericAPIView/APIView, the Browsable API, and OpenAPI schema generation (via drf-spectacular)
  • Automatic query optimization: select_related, prefetch_related (including recursively-optimized nested Prefetch querysets), and only() are derived from the requested fields — see docs/performance.md for measured query-count reductions.
  • Safe by default: unrequested SerializerMethodFields are never invoked, so expensive computed fields only cost what they cost when a client actually asks for them.
  • Fully typed, PEP 561 compatible, mypy --strict clean.

Installation

pip install drf-partial-response-fields

# with OpenAPI schema support
pip install drf-partial-response-fields[openapi]

Requires Python 3.10+, Django 4.2+, and Django REST Framework 3.14+.

Quick Start

# serializers.py
from rest_framework import serializers
from drf_partial_response_fields import PartialFieldsModelSerializer


class AuthorSerializer(PartialFieldsModelSerializer):
    class Meta:
        model = Author
        fields = ["id", "name", "email"]


class ArticleSerializer(PartialFieldsModelSerializer):
    author = AuthorSerializer()

    class Meta:
        model = Article
        fields = ["id", "title", "body", "author"]
# views.py
from rest_framework import viewsets
from drf_partial_response_fields import PartialResponseMixin


class ArticleViewSet(PartialResponseMixin, viewsets.ModelViewSet):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer

That's it — GET /articles/?fields=title,author(name) now returns only title and author.name, and the underlying queryset automatically gets select_related("author").only("id", "title", "author__id", "author__name").

See docs/quickstart.md and docs/advanced-usage.md for aliasing, exclusion, SerializerMethodField optimization hints, and plain-APIView integration.

Documentation

Full documentation is available at Documentation Home Page, including:

Contributing

Contributions are welcome — see Contributing.

License

MIT — see LICENSE.

Metadata

Release files for drf-partial-response-fields 1.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-partial-response-fields 1.0.6
File Size Uploaded
drf_partial_response_fields-1.0.6.tar.gz 54.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-partial-response-fields 1.0.6
File Interpreter ABI Platform
drf_partial_response_fields-1.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 82.3 kB

Release files / drf_partial_response_fields-1.0.6.tar.gz

Download URL drf_partial_response_fields-1.0.6.tar.gz
Size 54.9 kB
Tags Source
SHA-256 checksum
How to use checksums
61eade512a7c8d3345690954f8d853c993f3932b2563f1ad532d97dd888fc6cf
BLAKE2b-256 checksum
How to use checksums
8de5de6a35cfba1dea00520c1a8337507ea65b04ebfa361836368fc88747cb3f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release files / drf_partial_response_fields-1.0.6-py3-none-any.whl

Download URL drf_partial_response_fields-1.0.6-py3-none-any.whl
Size 27.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7e79ad0a682ab3424aafa0d716c95e4552ad082a6365de5a0d9715f366b50295
BLAKE2b-256 checksum
How to use checksums
efa3cc09fc4e5d164bd2089b4adb6e1874e7f58b3f8f94175f0f737027ff5174
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release history Release notifications | RSS feed

This release

1.0.6 This release

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page