1
0
mirror of https://github.com/django/django.git synced 2024-12-23 09:36:06 +00:00

Fixed #34199 -- Added example to StringAgg docs.

This commit is contained in:
sdolemelipone 2022-12-03 11:14:57 +00:00 committed by Mariusz Felisiak
parent 0abd8f1cb8
commit 344593893b

View File

@ -208,6 +208,32 @@ General-purpose aggregation functions
Examples are the same as for :attr:`ArrayAgg.ordering`. Examples are the same as for :attr:`ArrayAgg.ordering`.
Usage example::
class Publication(models.Model):
title = models.CharField(max_length=30)
class Article(models.Model):
headline = models.CharField(max_length=100)
publications = models.ManyToManyField(Publication)
>>> article = Article.objects.create(headline="NASA uses Python")
>>> article.publications.create(title="The Python Journal")
<Publication: Publication object (1)>
>>> article.publications.create(title="Science News")
<Publication: Publication object (2)>
>>> from django.contrib.postgres.aggregates import StringAgg
>>> Article.objects.annotate(
... publication_names=StringAgg(
... "publications__title",
... delimiter=", ",
... ordering="publications__title",
... )
... ).values("headline", "publication_names")
<QuerySet [{
'headline': 'NASA uses Python', 'publication_names': 'Science News, The Python Journal'
}]>
.. deprecated:: 4.0 .. deprecated:: 4.0
If there are no rows and ``default`` is not provided, ``StringAgg`` If there are no rows and ``default`` is not provided, ``StringAgg``