Skip to main content
https://img.shields.io/travis/kako-nawao/django-group-by.svg?style=plastic https://img.shields.io/coveralls/kako-nawao/django-group-by.svg?style=plastic

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 this repository:

pip install -e https://github.com/kako-nawao/django-group-by.git

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>

Download files

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

Source Distribution

django_group_by-0.1.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_group_by-0.1.1-py2.py3-none-any.whl (6.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django_group_by-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django_group_by-0.1.1.tar.gz
Algorithm Hash digest
SHA256 71a18b2e602b677d66bbe2e7af4688021a21bad4ff66a555b6177c030f59a9a9
MD5 0979caa02f89cb552c040c0c948d4353
BLAKE2b-256 b9659822ef79ab8387564f22cd0870775a9e479a51d8540892218fced144bd41

See more details on using hashes here.

File details

Details for the file django_group_by-0.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_group_by-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d8b1019a7da1ac46afe3ddc8595c9e44f53bbd87e1513ca0a2e02154c40e9b2f
MD5 b426ee3c93139184e1006c606fbb134f
BLAKE2b-256 54b8f164fab12da2bf13d2b7f72396ea37e829cddd059f5584fd8002d2275e93

See more details on using hashes here.

Release history Release notifications | RSS feed

0.3.1

2 files

0.3.0

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

This release

0.1.1 This release

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page