{"id":23799447,"url":"https://github.com/singularit-de/django-rest-aggregation","last_synced_at":"2025-09-06T13:32:54.042Z","repository":{"id":209856141,"uuid":"725096000","full_name":"singularit-de/django-rest-aggregation","owner":"singularit-de","description":null,"archived":false,"fork":false,"pushed_at":"2024-10-29T09:43:07.000Z","size":73,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-12-30T23:35:54.073Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/singularit-de.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-29T12:34:53.000Z","updated_at":"2024-10-29T09:42:01.000Z","dependencies_parsed_at":"2024-02-13T18:10:34.463Z","dependency_job_id":"bfac16a9-2b4f-4cac-bb95-4e7b25b4365e","html_url":"https://github.com/singularit-de/django-rest-aggregation","commit_stats":null,"previous_names":["singularit-de/django-rest-aggregation"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/singularit-de%2Fdjango-rest-aggregation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/singularit-de%2Fdjango-rest-aggregation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/singularit-de%2Fdjango-rest-aggregation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/singularit-de%2Fdjango-rest-aggregation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/singularit-de","download_url":"https://codeload.github.com/singularit-de/django-rest-aggregation/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232126174,"owners_count":18476190,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-01T21:16:35.610Z","updated_at":"2025-01-01T21:16:36.263Z","avatar_url":"https://github.com/singularit-de.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"You can find the original Project here: https://github.com/kit-oz/drf-aggregation\n\n# Django Rest Framework Aggregation\n\n---\n\nA simple Django Rest Framework extension to add aggregation endpoints to your API.\n\nKey features:\n\n- count, sum, average, minimum, maximum\n- grouping by multiple fields\n- filtering and ordering\n\n---\n\n## Installation\n\nTo install, use pip\n\n    pip install django-rest-aggregation\n\n## Quickstart\n\nInherit the **AggregationMixin** in your ViewSet.\n\n```python\nfrom django_rest_aggregation.mixins import AggregationMixin\n\n...\n\nclass BookViewSet(GenericViewSet, AggregationMixin):\n    queryset = Book.objects.all()\n    serializer_class = BookSerializer\n```\n\nRegister the ViewSet in your `urls.py`. The default aggregation endpoint is **{base_url}/aggregation/**.\n\n```python\nrouter = DefaultRouter()\nrouter.register(r'book', BookViewSet)\n\nurlpatterns = [\n    path('', include(router.urls)),\n    # or\n    path('book/custom/endpoint/', BookViewSet.as_view({'get': 'aggregation'}))\n]\n```\n\nGet the aggregation results by sending a GET request to the aggregation endpoint.\n\n| URL                                                                                             | What it does                                                            |\n|-------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------|\n| ```/book/aggregation/?aggregation=count ```                                                     | Get the total number of books                                           |\n| ```/book/aggregation/?aggregation=maximum\u0026aggregation_field=price```                            | Get the most expensive book                                             |\n| ```/book/aggregation/?aggregation=average\u0026aggregation_field=rating\u0026group_by=author```           | Get the average rating grouped by author                                |\n| ```/book/aggregation/?aggregation=sum\u0026aggregation_field=pages\u0026group_by=author\u0026value__gt=1000``` | Get the sum of all pages grouped by authors which are greater than 1000 |\n\n## Parameter overview\n\n### URL Parameters\n\n| query parameter   | description                                | example                       |\n|-------------------|--------------------------------------------|-------------------------------|\n| aggregation       | determines the type of aggregation         | ```aggregation=sum```         |\n| aggregation_field | the field to aggregate on                  | ```aggregation_field=price``` |\n| group_by          | the field on which the queryset is grouped | ```group_by=author```         |\n| value             | the value to filter the queryset           | ```value__gt=1000```          |\n\n### View Class Variables\n\n| class variable               | description                                          | example                                             |\n|------------------------------|------------------------------------------------------|-----------------------------------------------------|\n| aggregation_name             | changes the default value name                       | ```aggregation_name=foo```                          |\n| aggregation_serializer_class | Sets a custom serializer for the queryset            | ```aggregation_serializer_class=CustomSerializer``` |\n| aggregated_filtering_class   | Sets a FilterSet Class for filtering the value field | ```aggregated_filtering_class=ValueFilter```        |\n| aggregated_filterset_fields  | A shortcut for setting a value Filtersets            | ```aggregated_filterset_fields=[lt, lte, gt]```     |\n\n## Aggregations\n\nAvailable aggregations are:\n\n- count\n- sum\n- average\n- minimum\n- maximum\n\nThey can be used by adding the mandatory **aggregation** parameter to the request URL.\n\n        /book/aggregation/?aggregation=count\n\n### Aggregation Field\n\nThe aggregation field is the field to aggregate on.\nIt can be used by adding the **aggregation_field** parameter to the request URL.\nThis is a mandatory parameter for the **sum**, **average**, **minimum** and **maximum** aggregations.\nBoth model fields and annotated fields can be used.\n\n        /book/aggregation/?aggregation=sum\u0026aggregation_field=price\n\nYou can also use the double underscore notation to aggregate on related model fields.\n\n        /book/aggregation/?aggregation=sum\u0026aggregation_field=author__age\n\nIf the aggregation is sum or average, the aggregation field must be a numeric field. If the aggregation is min or\nmax, the aggregation field must be date or numeric field.\n\n## Grouping\n\nGrouping is done by adding the **group_by** parameter to the request URL.\nAgain, model fields and annotated fields can be used.\n\n        /book/aggregation/?aggregation=count\u0026group_by=author\n\nYou can group throughout multiple model relations (ForeignKey \u0026 ManyToMany Fields) by using the double underscore\nnotation.\n\n        /store/aggregation/?aggregation=count\u0026group_by=books__author__age\n\nTo group by multiple fields, separate them with a comma.\n\n        /book/aggregation/?aggregation=count\u0026group_by=author,genre\n\n## Filtering \u0026 Ordering\nTo filter the queryset, you can use the standard Django Filter Backend.\nThat implies:\n - filtering before aggregation\n\n        /book/aggregation/?aggregation=count\u0026group_by=author\u0026pages__gt=100\n\n -  filtering after aggregation\n\n        /book/aggregation/?aggregation=count\u0026group_by=author\u0026value__gt=5\n - combined filtering\n\n        /book/aggregation/?aggregation=count\u0026group_by=author\u0026pages__gt=100\u0026value__gt=100\n\nTo control which filtering options are available, you can use the **aggregated_filtering_class** class variable.\nThis sets a custom FilterSet Class for filtering the value field.\n\n```python\nclass BookViewSet(GenericViewSet, AggregationMixin):\n    queryset = Book.objects.all()\n    serializer_class = BookSerializer\n    aggregated_filtering_class = ValueFilter\n\nclass ValueFilter(filters.FilterSet):\n    value__gte = filters.NumberFilter(field_name='test123', lookup_expr='gte')\n    value__lte = filters.NumberFilter(field_name='test123', lookup_expr='lte')\n\n    class Meta:\n        fields = ['test123__gte', 'test123__lte']\n```\nA shortcut for setting value Filtersets is the **aggregated_filtering_fields** class variable.\nThis automatically creates a FilterSet class for filtering the value field.\n\n```python\nclass BookViewSet(GenericViewSet, AggregationMixin):\n    queryset = Book.objects.all()\n    serializer_class = BookSerializer\n    aggregated_filtering_fields = ['lt', 'lte', 'gt']\n```\nYou can use 'lt', 'lte', 'gt', 'gte', 'exact' and  \u0026#95;\u0026#95;all\u0026#95;\u0026#95; as values for the **aggregated_filtering_fields** class variable.\n\nFields available for ordering the result are taken from the `aggregated_ordering_fields` attribute.\n```python\nclass BookViewSet(GenericViewSet, AggregationMixin):\n    queryset = Book.objects.all()\n    serializer_class = BookSerializer\n    aggregated_ordering_fields = ['value', 'grouped_by_field']\n```\nif this is not set available fields are taken from the usual `ordering_fields` attribute.\nThe reason for this behaviour is to accommodate interplay with the often used `OrderingFilter` \n```python\nclass BookViewSet(GenericViewSet, AggregationMixin):\n    queryset = Book.objects.all()\n    serializer_class = BookSerializer\n    ordering_fields = ['page_count', 'author']\n```\nIf no ordering fields of any kind are specified or the list contains `\"__all__\"` any passed ordering will be tried to be applied.\n``ordering_fields = __all__`` is also possible.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsingularit-de%2Fdjango-rest-aggregation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsingularit-de%2Fdjango-rest-aggregation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsingularit-de%2Fdjango-rest-aggregation/lists"}