Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kako-nawao/django-group-by
Have your valuesquerysets return model instances instead of dicts
https://github.com/kako-nawao/django-group-by
Last synced: 2 months ago
JSON representation
Have your valuesquerysets return model instances instead of dicts
- Host: GitHub
- URL: https://github.com/kako-nawao/django-group-by
- Owner: kako-nawao
- License: mit
- Created: 2015-11-20T17:18:04.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2019-02-18T16:35:40.000Z (almost 6 years ago)
- Last Synced: 2024-09-22T19:45:51.382Z (4 months ago)
- Language: Python
- Size: 33.2 KB
- Stars: 25
- Watchers: 3
- Forks: 2
- Open Issues: 8
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
- starred-awesome - django-group-by - Have your valuesquerysets return model instances instead of dicts (Python)
README
==============
Django GroupBy
==============.. image:: https://img.shields.io/github/license/kako-nawao/django-group-by.svg
:target: http://www.opensource.org/licenses/MIT.. image:: https://img.shields.io/pypi/pyversions/django-group-by.svg
:target: https://pypi.python.org/pypi/django-group-by
.. image:: https://img.shields.io/pypi/v/django-group-by.svg
:target: https://pypi.python.org/pypi/django-group-by.. image:: https://img.shields.io/travis/kako-nawao/django-group-by.svg
:target: https://travis-ci.org/kako-nawao/django-group-by
.. image:: https://img.shields.io/codecov/c/github/kako-nawao/django-group-by.svg
:target: https://codecov.io/gh/kako-nawao/django-group-byThis 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 GroupByMixinclass BookQuerySet(QuerySet, GroupByMixin):
passclass 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()
4The difference is that every row is not a dictionary but an AggregatedGroup instance, with only the grouped fields::
>>> row = some_rows[0]
>>> row
>>> 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
>>> row.author_nationality
Related Model ID Only
~~~~~~~~~~~~~~~~~~~~~The previous example shows a difference in behaviour from Django's ``values``: we're grouping by the foreign key
*author*, but instead of getting only the ID we get the full instance. Why? Because it's more useful, and I
think that getting *{'author': 5}* as a result is just weird.If you just want the ID you can specify it::
>>> some_rows = Book.objects.group_by('title', 'author_id', 'author__nationality_id').distinct()