Package: django-ormex
Django ORM extensions.PrerequisitesDjango 1.11, 2.2 and 3.0.Python 2.7, 3.5, 3.6, 3.7 and 3.8Supported databasesPostgreSQL, MySQL, SQLiteInstallationInstall in your virtual environment.Latest stable version from PyPI:pipinstalldjango-ormexLatest stable version from GitHub:pipinstallhttps://github.com/barseghyanartur/django-ormex/archive/stable.tar.gzUsageCommon usage examples.AggregationsContains various modules for aggregations.GroupConcatWorks like Concat, but for concatenating field values of related ManyToMany
model. For instance, you may use it if you have anAuthormodel as
ManyToMany relation in theBookmodel
(Book.authors = ManyToManyField(Author)) and you want to have concatenated
list of all authors coupled to a given book.Given the following models:classPublisher(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)classAuthor(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)classBook(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:fromormex.aggregationsimportGroupConcatbook=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.'}GroupConcataccepts an optional argumentorder_bywhich can be used
for tuning the sorting order of the resulted list of strings. In case ifselfis 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.'}DemoRun demo locallyIn order to be able to quickly evaluate thedjango-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 latestormex_demo_installer.sh:wget-O-https://raw.github.com/barseghyanartur/django-ormex/stable/examples/ormex_demo_installer.sh|bashOpen your browser and test the app.URL:http://127.0.0.1:8001/If quick installer doesn’t work for you, see the manual steps on running theexample project.TestingSimply type:./runtests.pyor use tox:toxor use tox to check specific env:tox-epy35or run Django tests:./manage.pytestormex--settings=settings.testingLicenseGPL-2.0-only OR LGPL-2.1-or-laterSupportFor any issues contact me at the e-mail given in theAuthorsection.AuthorArtur Barseghyan <artur.barseghyan@gmail.com>