Skip to main content

Django ORM extensions.

PyPI Version Supported Python versions Build Status GPL-2.0-only OR LGPL-2.1-or-later Coverage

Prerequisites

  • Django 1.11, 2.2 and 3.0.

  • Python 2.7, 3.5, 3.6, 3.7 and 3.8

Supported databases

PostgreSQL, MySQL, SQLite

Installation

Install in your virtual environment.

Latest stable version from PyPI:

pip install django-ormex

Latest stable version from GitHub:

pip install https://github.com/barseghyanartur/django-ormex/archive/stable.tar.gz

Usage

Common usage examples.

Aggregations

Contains various modules for aggregations.

GroupConcat

Works like Concat, but for concatenating field values of related ManyToMany model. For instance, you may use it if you have an Author model as ManyToMany relation in the Book model (Book.authors = ManyToManyField(Author)) and you want to have concatenated list of all authors coupled to a given book.

Given the following models:

class Publisher(models.Model):
    """Publisher."""

    name = models.CharField(max_length=255)
    address = models.CharField(max_length=255)
    city = models.CharField(max_length=255)
    state_province = models.CharField(max_length=255)
    country = models.CharField(max_length=255)
    website = models.URLField(max_length=255)


class Author(models.Model):
    """Author."""

    salutation = models.CharField(max_length=255)
    name = models.CharField(max_length=255)
    email = models.EmailField(max_length=255)
    headshot = models.ImageField(upload_to='authors', null=True, blank=True)


class Book(models.Model):
    """Book."""

    title = models.CharField(max_length=255)
    authors = models.ManyToManyField('books.Author', related_name='books')
    publisher = models.ForeignKey(Publisher, related_name='books')
    publication_date = models.DateField()
    isbn = models.CharField(max_length=255, unique=True)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    pages = models.PositiveIntegerField(default=200)
    stock_count = models.PositiveIntegerField(default=30)

We could use GroupConcat as follows:

from ormex.aggregations import GroupConcat

book = Book.objects.all() \
        .values('id',
                'title',
                'pages',
                'price',
                'publisher__id',
                'publisher__name') \
        .annotate(
            authors__name=GroupConcat('authors__name', separator=', ')
        ) \
        .first()

Output would look as follows:

{
    'authors__name': 'Finn Janssen, Dan Dijkman, Merel Wolf, Evy de Jong',
    'id': 14,
    'pages': 83,
    'price': Decimal('62.13'),
    'publisher__id': 19,
    'publisher__name': 'Rijn, de Bruyn and Verharen',
    'title': 'Laboriosam officia temporibus facere omnis odit.'
}

GroupConcat accepts an optional argument order_by which can be used for tuning the sorting order of the resulted list of strings. In case if self is given as value, sorted by the same field. In order to sort the list of authors by name from the example above, do:

book = Book.objects.all() \
        .values('id',
                'title',
                'pages',
                'price',
                'publisher__id',
                'publisher__name') \
        .annotate(
            authors__name=GroupConcat('authors__name',
                                      separator=', ',
                                      order_by='self')
        ) \
        .first()

Output would look as follows:

{
    'authors__name': 'Dan Dijkman, Evy de Jong, Finn Janssen, Merel Wolf',
    'id': 14,
    'pages': 83,
    'price': Decimal('62.13'),
    'publisher__id': 19,
    'publisher__name': 'Rijn, de Bruyn and Verharen',
    'title': 'Laboriosam officia temporibus facere omnis odit.'
}

Demo

Run demo locally

In order to be able to quickly evaluate the django-ormex, a demo app (with a quick installer) has been created (works on Ubuntu/Debian, may work on other Linux systems as well, although not guaranteed). Follow the instructions below to have the demo running within a minute.

Grab the latest ormex_demo_installer.sh:

wget -O - https://raw.github.com/barseghyanartur/django-ormex/stable/examples/ormex_demo_installer.sh | bash

Open your browser and test the app.

If quick installer doesn’t work for you, see the manual steps on running the example project.

Testing

Simply type:

./runtests.py

or use tox:

tox

or use tox to check specific env:

tox -e py35

or run Django tests:

./manage.py test ormex --settings=settings.testing

License

GPL-2.0-only OR LGPL-2.1-or-later

Support

For any issues contact me at the e-mail given in the Author section.

Author

Artur Barseghyan <artur.barseghyan@gmail.com>

Release files for django-ormex 0.2.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-ormex 0.2.1
File Size Uploaded
django-ormex-0.2.1.tar.gz 22.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-ormex 0.2.1
File Interpreter ABI Platform
django_ormex-0.2.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 49.1 kB

Release files / django-ormex-0.2.1.tar.gz

Download URL django-ormex-0.2.1.tar.gz
Size 22.1 kB
Tags Source
SHA-256 checksum
How to use checksums
4fd22a23a22d6c478957e176603259a2197a9cd8091c61fd1464aa7a3c520f83
BLAKE2b-256 checksum
How to use checksums
5d73a815c1d449917e0590fa905a4f2b6076a24fbf2f21c1ebe9fe2c3a27e4c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.6

Release files / django_ormex-0.2.1-py2.py3-none-any.whl

Download URL django_ormex-0.2.1-py2.py3-none-any.whl
Size 27.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
6a8781ed682eb3044a56812f023506f3533e7acb23f0266a36b9060fd89855e7
BLAKE2b-256 checksum
How to use checksums
561b6fe3966e088313b0f293ce9e781aeaaa7998c553cabec4ea602a5863ac77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.6

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2

2 release files

0.1.1

2 release files

0.1

2 release files

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