Skip to main content

A Python package that exposes the Django model queryset aggregate functions to the DRF API.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

drf_aggregates-0.0.12-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

drf_aggregates-0.0.12-py2-none-any.whl (5.9 kB view details)

Uploaded Python 2

File details

Details for the file drf_aggregates-0.0.12-py3-none-any.whl.

File metadata

  • Download URL: drf_aggregates-0.0.12-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for drf_aggregates-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 1a3be72caaee7b38764a69e982926d3fede3ec217c0500909e1d42c70b347672
MD5 6fc86706ce55eb2fa27b7ae014e96136
BLAKE2b-256 4926598863043a9ca25b6fca49c7637aca3fed4abb54e458fc15b6800ef9690f

See more details on using hashes here.

File details

Details for the file drf_aggregates-0.0.12-py2-none-any.whl.

File metadata

  • Download URL: drf_aggregates-0.0.12-py2-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 2
  • Uploaded using 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

File hashes

Hashes for drf_aggregates-0.0.12-py2-none-any.whl
Algorithm Hash digest
SHA256 c8a3da60bf4c7c390dfafddc770272ad28d3f9f9019d2cc35e23939af930f63c
MD5 46c9c7995d69424774b157a8b897e21d
BLAKE2b-256 60ac0613f40af4b69c2f181b46bc7c6a06ff44f974351d0fe8c43a326416c968

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page