Coder Social home page Coder Social logo

liradb2000 / django-rest-framework-aggregates Goto Github PK

View Code? Open in Web Editor NEW

This project forked from uptick/django-rest-framework-aggregates

0.0 0.0 0.0 32 KB

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

Python 100.00%

django-rest-framework-aggregates's Introduction

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

django-rest-framework-aggregates's People

Contributors

jarekwg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.