Django Orm Extensions
django-ormex的Python项目详细描述
django orm扩展。
先决条件
- Django 1.8、1.9、1.10、1.11
- Python2.7、3.4、3.5、3.6
安装
在虚拟环境中安装。
来自PYPI的最新稳定版本:
pip install django-ormex
来自Github的最新稳定版本:
pip install https://github.com/barseghyanartur/django-ormex/archive/stable.tar.gz
用法
常见用法示例。
聚集
包含用于聚合的各种模块。
组连接
工作方式与concat类似,但用于连接相关manytomyne的字段值 模型。例如,如果有一个Author模型 Book模型中的多个关系 (Book.authors = ManyToManyField(Author))并且您希望连接 与给定书籍耦合的所有作者的列表。
提供以下型号:
classPublisher(models.Model):"""Publisher."""name=models.CharField(max_length=30)address=models.CharField(max_length=50)city=models.CharField(max_length=60)state_province=models.CharField(max_length=30)country=models.CharField(max_length=50)website=models.URLField()classAuthor(models.Model):"""Author."""salutation=models.CharField(max_length=10)name=models.CharField(max_length=200)email=models.EmailField()headshot=models.ImageField(upload_to='authors',null=True,blank=True)classBook(models.Model):"""Book."""title=models.CharField(max_length=100)authors=models.ManyToManyField('books.Author',related_name='books')publisher=models.ForeignKey(Publisher,related_name='books')publication_date=models.DateField()isbn=models.CharField(max_length=100,unique=True)price=models.DecimalField(max_digits=10,decimal_places=2)pages=models.PositiveIntegerField(default=200)stock_count=models.PositiveIntegerField(default=30)
我们可以使用groupconcat,如下所示:
fromormex.aggregationsimportGroupConcatbook=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.'}
演示
在本地运行演示
为了能够快速评估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
打开浏览器并测试应用程序。
如果快速安装程序不适合您,请参阅有关运行 example project。
测试
只需键入:
./runtests.py
或使用毒物:
tox
或使用毒性检查特定环境:
tox -e py35
或者运行django测试:
./manage.py test ormex --settings=settings.testing
许可证
gpl 2.0/lgpl 2.1
支架
如有任何问题,请通过Author部分中给出的电子邮件与我联系。