Skip to main content

Django GroupBy

https://img.shields.io/github/license/kako-nawao/django-group-by.svg https://img.shields.io/pypi/pyversions/django-group-by.svg https://img.shields.io/pypi/v/django-group-by.svg https://img.shields.io/travis/kako-nawao/django-group-by.svg https://img.shields.io/codecov/c/github/kako-nawao/django-group-by.svg

This package provides a mixin for Django QuerySets that adds a method group_by that behaves mostly like the values method, but with one difference: its iterator does not return dictionaries, but a model-like object with model instances for related values.

Installation

Install from PyPI:

pip install django-group-by

Compatibility

This package is compatible with Django 1.8, 1.9 and 1.10, and Python versions 2.7, 3.4, 3.5 and 3.6. Probably others, but those 12 combinations are the ones against which we build (by Travis CI).

Usage

Create a QuerySet subclass with the GroupByMixin to use in a model’s manager:

# models.py

from django.db import models
from django.db.models.query import QuerySet
from django_group_by import GroupByMixin

class BookQuerySet(QuerySet, GroupByMixin):
    pass

class Book(Model):
    objects = BookQuerySet.as_manager()

    title = models.CharField(max_length=100)
    author = models.ForeignKey('another_app.Author')
    publication_date = models.DateField()
...

Then use it just like values, and you’ll get a similar query set:

>>> some_rows = Book.objects.group_by('title', 'author', 'author__nationality').distinct()
>>> some_rows.count()
4

The difference is that every row is not a dictionary but an AggregatedGroup instance, with only the grouped fields:

>>> row = some_rows[0]
>>> row
<AggregatedGroup for Book>
>>> row.title
The Colour of Magic
>>> row.publication_date
*** AttributeError: 'AggregatedGroup' object has no attribute 'publication_date'

But of course the main advantage is that you also get related model instances, as far as you want:

>>> row.author
<Author: Terry Pratchett>
>>> row.author_nationality
<Nation: Great Britain>

Metadata

Release files for django-group-by 0.3.1

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

Source distribution (sdist)

Source distribution for django-group-by 0.3.1
File Size Uploaded Provenance Actions
django_group_by-0.3.1.tar.gz 5.5 kB Without provenance Details for django_group_by-0.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-group-by 0.3.1
File Interpreter ABI Platform Actions
django_group_by-0.3.1-py2.py3-none-any.whl Python 2, Python 3 none any Details for django_group_by-0.3.1-py2.py3-none-any.whl

Total release size: 14.1 kB

Release files / django_group_by-0.3.1.tar.gz

Download URL django_group_by-0.3.1.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4f1a17b61c7d16924a1c7afcea3ceb42c217c5c845fd03ad6e54fda4b6c710db
BLAKE2b-256 checksum
How to use checksums
c80ab98b8fbb9cd8879e13b5bfebcdda90e5d2490b90bacfd272dbfc6f7d6ad7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_group_by-0.3.1-py2.py3-none-any.whl

Download URL django_group_by-0.3.1-py2.py3-none-any.whl
Size 8.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b4e554c0eb9eed39c0cdc64de20fa7b2906ad92408d6a47b5bc55764711fc2e5
BLAKE2b-256 checksum
How to use checksums
efeae44706b97580d021d6e175c2a186e05c6eb05ae8b034cc3d490b99b32862
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for django-group-by 0.3.1

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