Supported versions
Only Django and Python versions that upstream still supports are tested and supported:
| Django | Python | Django REST framework |
|---|---|---|
| 5.2 LTS | 3.10 – 3.14 | 3.16+ |
| 6.0 | 3.12 – 3.14 | 3.16+ |
| 6.1 | 3.12 – 3.14 | 3.16+ |
Django rest framework sideloading
DRF-sideloading is an extension to provide side-loading functionality of related resources. Side-loading allows related resources to be optionally included in a single API response minimizing requests to the API.
Quickstart
-
Install drf-sideloading:
pip install drf-sideloading
OpenAPI schema generation is optional. Install the
spectacularextra to have thesideloadquery parameter documented by drf-spectacular:pip install "drf-sideloading[spectacular]"
-
Import
SideloadableRelationsMixin:from drf_sideloading.mixins import SideloadableRelationsMixin
-
Write your SideLoadableSerializer:
You need to define the primary serializer in the Meta data and can define prefetching rules. Also notice the many=True on the sideloadable relationships.
from drf_sideloading.serializers import SideLoadableSerializer class ProductSideloadableSerializer(SideLoadableSerializer): products = ProductSerializer(many=True) categories = CategorySerializer(source="category", many=True) primary_suppliers = SupplierSerializer(source="primary_supplier", many=True) secondary_suppliers = SupplierSerializer(many=True) suppliers = SupplierSerializer(many=True) partners = PartnerSerializer(many=True) class Meta: primary = "products" prefetches = { "categories": "category", "primary_suppliers": "primary_supplier", "secondary_suppliers": "secondary_suppliers", "suppliers": { "primary_suppliers": "primary_supplier", "secondary_suppliers": "secondary_suppliers", }, "partners": "partners", }
-
Prefetches
For fields where the source is provided or where the source matches the field name, prefetches are not strictly required
Multiple prefetches can be added to a single sideloadable field, but when using Prefetch object check that they don't clash with prefetches made in the get_queryset() method
from django.db.models import Prefetch prefetches = { "categories": "category", "primary_suppliers": ["primary_supplier", "primary_supplier__some_related_object"], "secondary_suppliers": Prefetch( lookup="secondary_suppliers", queryset=Supplier.objects.prefetch_related("some_related_object") ), "partners": Prefetch(lookup="partners", queryset=Partner.objects.select_related("some_related_object")), }
Multiple sources can be added to a field using a dict. Each key is a source_key that can be used to filter what sources should be sideloaded. The values set the source and prefetches for this source.
Note that this prefetch reuses
primary_supplierandsecondary_suppliersif suppliers and primary_supplier or secondary_suppliers are sideloadedprefetches = { "primary_suppliers": "primary_supplier", "secondary_suppliers": "secondary_suppliers", "suppliers": {"primary_suppliers": "primary_supplier", "secondary_suppliers": "secondary_suppliers"}, }
Usage of Prefetch() objects is supported. Prefetch() objects can be used to filter a subset of some relations or just to prefetch or select complicated related objects In case there are prefetch conflicts,
to_attrcan be set but be aware that this prefetch will now be a duplicate of similar prefetches. prefetch conflicts can also come from prefetched made in the ViewSet.get_queryset() method.Note that this prefetch noes not reuse
primary_supplierandsecondary_suppliersif suppliers and primary_supplier or secondary_suppliers are sideloaded at the same time.from django.db.models import Prefetch prefetches = { "categories": "category", "primary_suppliers": "primary_supplier", "secondary_suppliers": "secondary_suppliers", "suppliers": { "primary_suppliers": Prefetch( lookup="secondary_suppliers", queryset=Supplier.objects.select_related("some_related_object"), to_attr="secondary_suppliers_with_preselected_relation", ), "secondary_suppliers": Prefetch( lookup="secondary_suppliers", queryset=Supplier.objects.filter(created_at__gt=pendulum.now().subtract(days=10)).order_by("created_at"), to_attr="latest_secondary_suppliers", ), }, }
-
Configure sideloading in ViewSet:
Include SideloadableRelationsMixin mixin in ViewSet and define sideloading_serializer_class as shown in example below. Everything else stays just like a regular ViewSet. Since version 2.0.0 there are 3 new methods that allow to overwrite the serializer used based on the request version for example Since version 2.1.0 an additional method was added that allow to add request dependent filters to sideloaded relations
from drf_sideloading.mixins import SideloadableRelationsMixin class ProductViewSet(SideloadableRelationsMixin, viewsets.ModelViewSet): """ A simple ViewSet for viewing and editing products. """ queryset = Product.objects.all() serializer_class = ProductSerializer sideloading_serializer_class = ProductSideloadableSerializer def get_queryset(self): # Add prefetches for the viewset as normal return super().get_queryset().prefetch_related("created_by") def get_sideloading_serializer_class(self, request=None): # use a different sideloadable serializer for older version if self.request.version < "1.0.0": return OldProductSideloadableSerializer return super().get_sideloading_serializer_class(request=request) def get_sideloading_serializer(self, *args, **kwargs): # if modifications are required to the serializer initialization this method can be used. return super().get_sideloading_serializer(*args, **kwargs) def get_sideloading_serializer_context(self): # Extra context provided to the serializer class. return {"request": self.request, "format": self.format_kwarg, "view": self} def add_sideloading_prefetch_filter(self, source, queryset, request): # if source == "model1__relation1": return queryset.filter(is_active=True), True if hasattr(queryset, "readable"): return queryset.readable(user=request.user), True return queryset, False
-
Enjoy your API with sideloading support
Example request and response when fetching all possible values
GET /api/products/?sideload=categories,partners,primary_suppliers,secondary_suppliers,suppliers,products{ "products": [ { "id": 1, "name": "Product 1", "category": 1, "primary_supplier": 1, "secondary_suppliers": [2, 3], "partners": [1, 2, 3] } ], "categories": [ { "id": 1, "name": "Category1" } ], "primary_suppliers": [ { "id": 1, "name": "Supplier1" } ], "secondary_suppliers": [ { "id": 2, "name": "Supplier2" }, { "id": 3, "name": "Supplier3" } ], "suppliers": [ { "id": 1, "name": "Supplier1" }, { "id": 2, "name": "Supplier2" }, { "id": 3, "name": "Supplier3" } ], "partners": [ { "id": 1, "name": "Partner1" }, { "id": 2, "name": "Partner1" }, { "id": 3, "name": "Partner3" } ] }
The user can also select what sources to load to Multi source fields. Leaving the selections empty or omitting the brackets will load all the prefetched sources.
Example:
GET /api/products/?sideload=suppliers[primary_suppliers]{ "products": [ { "id": 1, "name": "Product 1", "category": 1, "primary_supplier": 1, "secondary_suppliers": [2, 3], "partners": [1, 2, 3] } ], "suppliers": [ { "id": 1, "name": "Supplier1" } ] }
Example Project
Directory example contains an example project using django rest framework sideloading library. You can set it up and run it locally using following commands:
cd example
sh scripts/devsetup.sh
sh scripts/dev.sh
Contributing
Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.
This project uses uv for dependency management and packaging.
Setup for contribution
uv sync
uv creates the virtualenv, installs the project with every development dependency from
uv.lock, and downloads a suitable Python interpreter if one is missing. There is no
pip install step and no virtualenv to activate — prefix commands with uv run.
Test
$ make test # or: uv run pytest tests/
Run tests against a specific Django version
$ uv run --python 3.12 --with 'Django>=6.0,<6.0.99' pytest tests/ -v
The full Python/Django matrix runs in CI on every pull request; see
.github/workflows/build.yml.
Lint
$ make lint # ruff check + ruff format --check
$ make format # apply the fixes
Release
Bump the version — this commits and tags — then push and publish a GitHub release. CI builds the distributions and uploads them to PyPI via trusted publishing.
$ uv run bump-my-version bump patch # or minor / major
$ git push --follow-tags
License
Credits
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file drf_sideloading-2.3.0.tar.gz.
File metadata
- Download URL: drf_sideloading-2.3.0.tar.gz
- Upload date:
- Size: 24.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ade8c703c294e7ce5c33389c985b98332c5aad24057ee38edb2bfae86a58edad
|
|
| MD5 |
c621ec4c3ee886817935728f42bb2cf4
|
|
| BLAKE2b-256 |
25b794bb7539937e8baeef3c67eaa45a65e2e7114e4d053794dbd576363c84ed
|
Provenance
The following attestation bundles were made for drf_sideloading-2.3.0.tar.gz:
Publisher:
build.yml on namespace-ee/django-rest-framework-sideloading
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
drf_sideloading-2.3.0.tar.gz -
Subject digest:
ade8c703c294e7ce5c33389c985b98332c5aad24057ee38edb2bfae86a58edad - Sigstore transparency entry: 2673056894
- Sigstore integration time:
-
Permalink:
namespace-ee/django-rest-framework-sideloading@3a308a21fd73cf75bd5fc80860d8f5f9eda5c451 -
Branch / Tag:
refs/tags/v2.3.0 - Owner: https://github.com/namespace-ee
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@3a308a21fd73cf75bd5fc80860d8f5f9eda5c451 -
Trigger Event:
release
-
Statement type:
File details
Details for the file drf_sideloading-2.3.0-py3-none-any.whl.
File metadata
- Download URL: drf_sideloading-2.3.0-py3-none-any.whl
- Upload date:
- Size: 15.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e08345692e55980b04f3614e8e4ef0107c9ca3a48fa430c0eb34b7ee4966f057
|
|
| MD5 |
0e43eae2a65d2d661113501c14db30f5
|
|
| BLAKE2b-256 |
56c535afb8a114c74f7370f5b18b87a1f8c5af3f2e6826b92e1c22a1700c71d4
|
Provenance
The following attestation bundles were made for drf_sideloading-2.3.0-py3-none-any.whl:
Publisher:
build.yml on namespace-ee/django-rest-framework-sideloading
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
drf_sideloading-2.3.0-py3-none-any.whl -
Subject digest:
e08345692e55980b04f3614e8e4ef0107c9ca3a48fa430c0eb34b7ee4966f057 - Sigstore transparency entry: 2673056904
- Sigstore integration time:
-
Permalink:
namespace-ee/django-rest-framework-sideloading@3a308a21fd73cf75bd5fc80860d8f5f9eda5c451 -
Branch / Tag:
refs/tags/v2.3.0 - Owner: https://github.com/namespace-ee
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@3a308a21fd73cf75bd5fc80860d8f5f9eda5c451 -
Trigger Event:
release
-
Statement type: