跳转到主要内容

Django ORM 扩展

项目描述

Django ORM 扩展。

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

先决条件

  • Django 1.11, 2.2 和 3.0。

  • Python 2.7, 3.5, 3.6, 3.7 和 3.8

支持的数据库

PostgreSQL, MySQL, SQLite

安装

在您的虚拟环境中安装。

来自PyPI的最新稳定版本

pip install django-ormex

来自GitHub的最新稳定版本

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

用法

常见用法示例。

聚合

包含用于聚合的各种模块。

GroupConcat

类似于Concat,但对于连接相关多对多模型的字段值。例如,如果您有一个Author模型作为Book模型中的多对多关系(Book.authors = ManyToManyField(Author))并且希望有一个与给定书籍关联的所有作者的连接列表。

给定以下模型

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)

我们可以这样使用GroupConcat

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()

输出将如下所示

{
    '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接受一个可选参数order_by,可用于调整字符串结果的排序顺序。如果给定为self,则按相同字段排序。为了按示例中上述作者名称排序,执行以下操作:

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

输出将如下所示

{
    '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.'
}

演示

在本地运行演示

为了能够快速评估 django-ormex,已创建了一个演示应用程序(带有快速安装程序)(在Ubuntu/Debian上运行,也可能在其他Linux系统上运行,尽管没有保证)。按照以下说明操作,可以在一分钟内运行演示。

获取最新的 ormex_demo_installer.sh

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

打开您的浏览器并测试应用程序。

如果快速安装程序对您不起作用,请参阅示例项目中的手动步骤。

测试

只需输入

./runtests.py

或使用tox

tox

或使用tox检查特定环境

tox -e py35

或运行Django测试

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

许可协议

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

支持

有关任何问题,请通过作者部分提供的电子邮件与我联系。

作者

Artur Barseghyan <artur.barseghyan@gmail.com>

项目详情


下载文件

下载适合您平台的应用程序。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

django-ormex-0.2.1.tar.gz (22.1 kB 查看散列)

上传时间

构建分布

django_ormex-0.2.1-py2.py3-none-any.whl (27.0 kB 查看散列)

上传时间 Python 2 Python 3

支持者: