DRF mixin library (AggregationMixin) that adds a GET …/aggregation/ action to ViewSets.
Published on PyPI as django-rest-aggregation, repo singularit-de/django-rest-aggregation. Small codebase,
about 300 lines in src/django_rest_aggregation/.
README.mdis only a signpost: short pitch, install, one example, links. It is also the PyPI description, so its links are absolute GitHub URLs (…/blob/main/Readme/<note>.md), never relative paths.Readme/holds atomic notes, one topic per file, linked to each other. Add new content as a new note or into the matching note, and link it fromREADME.md.Readme/issues.mdis the backlog of known vulnerabilities, bugs and packaging problems with IDs (SEC-n,BUG-n,PKG-n). It lists open issues only: reference the ID in the fixing commit and remove the entry (and links to it from other notes) when fixed. IDs are never reused. Keep the "verified / from code" status honest.- Docs are in English (public package).
uv sync # creates .venv: package, runtime deps, dev group
uv run runtests.py # = manage.py test with tests.settings; args are passed through
uv run coverage run runtests.py && uv run coverage report
uv run ruff check . && uv run ruff format . # line length 120 (ruff.toml); also runs as pre-commit hook and in CI
uv run ty check # type check of src/ ([tool.ty] in pyproject.toml); pre-commit hook and CI- uv only, no pip /
requirements*.txt. Dependencies are inpyproject.toml(dependencies,[dependency-groups]dev,postgres,mssql);uv.lockis committed. - Tests import the installed package (
django_rest_aggregation, editable viauv sync), notsrc.. - Database: in-memory SQLite by default;
DB_BACKEND=postgres|mysql|mariadb|mssqlplus optionalDB_NAME/DB_USER/DB_PASSWORD/DB_HOST/DB_PORTselect another one (tests/settings.py). - Test layout:
tests/settings.py, the test apptests/testapp/(models, views, urls, serializers, filters, pagination), the sharedsetUpof the filtering and ordering tests intests/base.py, tests intests/test_<topic>.py. - Without
orderingthe row order is not defined (PostgreSQL / SQL Server differ from SQLite). Tests that expect several rows pass a totalorderingongroup_byfields only; ordering on other fields changes the grouping.
All code in src/ is fully typed, and every new or changed function there must be too:
- Annotate every parameter and return type (ruff
ANNrules enforce this forsrc/; tests are exempt). uv run ty checkmust pass with no diagnostics; do not silence errors with# type: ignore/# ty: ignoreunless a third-party gap makes it unavoidable, and then say why in a comment.- Python 3.10 syntax only (
X | None,list[str]; notyping.Self, no PEP 695 generics). - Every module in
src/starts withfrom __future__ import annotations(ruffI002adds it), so annotations are never evaluated at runtime and are written without quotes. - Imports needed only for annotations go under
if TYPE_CHECKING:when importing them at runtime has side effects or requirements (e.g.django.contrib.contenttypes, which needs the app installed). Runtime expressions such ascast(...)arguments and type aliases are still evaluated, so they cannot use those names. - Use the existing types instead of re-describing them:
AggregationParams(parsed query parameters),FilterFields(aggregated_filterset_fields). The mixin is typed againstGenericAPIViewvia_MixinBase; new view attributes it reads get an annotation-only declaration onAggregationMixin. py.typedships with the package, so the types are part of the public API.
mixins.py:AggregationMixin.aggregation()runs view filters →Aggregator→ value filters → ordering → pagination → serializer.aggregator.py: parameter parsing, validation (field allowlist,group_bylimit, field types),.order_by().values(*group_by).annotate(name=Agg).filter.py:ValueFilterfor filters on the aggregated value; the mixin uses a fresh subclass per request (with_filter_fields()), never mutateValueFilteritself.enums.py: aggregation names and aliases.serializers.py: default output serializer, drops fields not ingroup_by/ value.
- The aggregated queryset is a
.values().annotate()queryset: anyorder_byfield outsidegroup_bybecomes part ofGROUP BYand silently produces wrong results.get_aggregated_queryset()clears earlier ordering andorder_aggregated_queryset()only accepts result columns; keep both (wrong groups, ordering oracle). - Every field path a client can name must go through
Aggregator.field_allowed()(allowlist fromget_aggregation_fields(), serializer fields by default, plus paths throughget_aggregation_models()). New features must not add data paths around it, or hidden fields leak; disallowed fields get the same error as unknown ones. - Public API names users rely on:
aggregation_name,aggregation_serializer_class,aggregated_filterset_class,aggregated_filterset_fields,aggregated_ordering_fields,aggregation_fields,aggregation_models,aggregation_max_group_by, the overridable methods on the mixin, and the query parametersaggregation,aggregation_field,group_by,ordering,<name>__<lookup>. Renames need deprecation aliases. - Runtime dependencies are lower bounds only (library, no
~=pins): Python ≥ 3.10, Django ≥ 5.2, DRF ≥ 3.16, django-filter ≥ 25.1 (first releases supporting Django 5.2). Keepclassifiersin sync when support changes. - Behaviour changes need a test in the matching
tests/test_<topic>.py(test app models:Agent,Author,Book,Publisher,Store; ViewSets intests/testapp/views.py). - CI (
ci.yml, every push) runs ruff and the tests (Python × Django matrix, PostgreSQL, SQL Server). - Release: bump
versioninpyproject.toml, publish a GitHub release →publish.ymlrunsci.ymlon the release tag and uploads to PyPI only if it passes. Saving a draft release does not trigger it. - Commit style: conventional prefixes (
feat:,fix:,chore:,refactor:,test:).