Skip to main content

django-rest-framework-aggregates

PyPI version Build Status

Exposes aggregation features of the Django model queryset to the DRF API.

Requirements

  • Python 3.6+
  • Django 1.11+
  • Django Rest Framework 3.5.3+

Overview

This renderer overwrites default behaviour for calls made to api v2 .agg endpoints.

Supports GET calls to list endpoints in the format:

endpoint.agg/?aggregate[Count]=(field to count)
endpoint.agg/?aggregate[Sum]=(field to sum)
endpoint.agg/?aggregate[custom_function]=arguments
endpoint.agg/?group_by[field to group by]&aggregate[Count]=id
endpoint.agg/?group_by[field to group by]&aggregate[Count]=id&aggregate[Sum]=(field to sum)

Supports date part extraction for aggregation:

endpoint.agg/?group_by[created__year]&aggregate[Count]=id

Supports choices to representation extract:

endpoint.agg/?group_by[choiceField]&aggregate[Count]=id

Custom Aggregations

The default aggregate functions supported are defined in django.db.models.aggregates.

Custom aggregate functions have been defined in drf_aggregates.aggregates

User defined aggregation are passed to a custom queryset manager calculate_aggregates as kwargs if defined.

Custom aggregate functions set on the queryset should return a dictionary of field names to aggregate functions, which will then be processed with the other aggregates.

Examples

Example setup can be found in the example/ folder.

To enable the renderer, update your Django settings file:

  REST_FRAMEWORK = {
      'DEFAULT_RENDERER_CLASSES': (
          'drf_aggregates.renderers.AggregateRenderer',
          ...
      ),
      ...
  }

In the Cars ViewSet we are outputting the result to json:

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        data = request.accepted_renderer.render({'queryset': queryset, 'request': request})
        return Response(data, content_type=f'application/json')

Tests

In order to run tests locally:

  1. Install development requirements:

    pip3 install -r requirements-dev.txt

  2. Update your environment to point to test Django settings file:

    export DJANGO_SETTINGS_MODULE=example.settings.test

  3. Run tests:

    py.test

Metadata

Release files for drf-aggregates 0.0.12

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

Built distributions (wheels)

Table of built distributions (wheels) for drf-aggregates 0.0.12
File Interpreter ABI Platform Actions
drf_aggregates-0.0.12-py3-none-any.whl Python 3 none any Details for drf_aggregates-0.0.12-py3-none-any.whl
drf_aggregates-0.0.12-py2-none-any.whl Python 2 none any Details for drf_aggregates-0.0.12-py2-none-any.whl

Total release size: 11.9 kB

Release files / drf_aggregates-0.0.12-py3-none-any.whl

Download URL drf_aggregates-0.0.12-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1a3be72caaee7b38764a69e982926d3fede3ec217c0500909e1d42c70b347672
BLAKE2b-256 checksum
How to use checksums
4926598863043a9ca25b6fca49c7637aca3fed4abb54e458fc15b6800ef9690f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.7

Release files / drf_aggregates-0.0.12-py2-none-any.whl

Download URL drf_aggregates-0.0.12-py2-none-any.whl
Size 5.9 kB
Tags Python 2
SHA-256 checksum
How to use checksums
c8a3da60bf4c7c390dfafddc770272ad28d3f9f9019d2cc35e23939af930f63c
BLAKE2b-256 checksum
How to use checksums
60ac0613f40af4b69c2f181b46bc7c6a06ff44f974351d0fe8c43a326416c968
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.7

Security for drf-aggregates 0.0.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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