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

Support aggregating over multiple fields, either of

endpoint.agg/?aggregate[Sum]=id&aggregate[Sum]=number
endpoint.agg/?aggregate[Sum]=id,number

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.13

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-aggregates 0.0.13
File Size Uploaded
drf-aggregates-0.0.13.tar.gz 5.7 kB Details

Built distribution (wheel)

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

Total release size: 11.7 kB

Release files / drf-aggregates-0.0.13.tar.gz

Download URL drf-aggregates-0.0.13.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
fc309bcd48eee3aed8a54504d627bd5523c4ceadbd0ebab4eb76300cbc863fbf
BLAKE2b-256 checksum
How to use checksums
cd5605b2fac5fe4d24b579bc97624eac7486eaf3c269924ec196a74287b16358
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.8.0 tqdm/4.50.0 CPython/3.8.5

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

Download URL drf_aggregates-0.0.13-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
629121252cca6e192c48964d1444049c2a4e868b4ad128ba4c123446a3ef0cc5
BLAKE2b-256 checksum
How to use checksums
418efcf8c8012207debdefc2fafafbdfe0376104bd79daaa91010f1420c26ec7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.8.0 tqdm/4.50.0 CPython/3.8.5

Release history Release notifications | RSS feed

This release

0.0.13 This release

2 release files

0.0.11

1 release file

0.0.10

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.5

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

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