Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#34199 closed Cleanup/optimization (fixed)

Add an example for contrib.postgres.aggregates.StringAgg to docs.

Reported by: Mark Gensler Owned by: Mark Gensler
Component: Documentation Version: dev
Severity: Normal Keywords: postgres StringAgg
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The documentation is missing an example for StringAgg. Also I thought it would be useful to include an example for ManyToMany fields, in contrast to the OneToMany example for JSONBAgg.

I.e.

class Publication(models.Model):
        title = models.CharField(max_length=30)

class Article(model.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')
>>> article.publications.create(title='Science News')

>>> 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'
]}]>

Change History (7)

comment:1 by Mark Gensler, 2 years ago

Has patch: set

comment:2 by David Sanders, 2 years ago

Hi Mark,

Thanks for the input!

Just FYI you don't need a ticket for trivial documentation updates. I think an example like this may fall under that category 👍

comment:3 by Mark Gensler, 2 years ago

Hi David, you're welcome! Thanks for letting me know, I will just make a PR for something like this in future.

Mark

comment:4 by Mariusz Felisiak, 2 years ago

Summary: Add documentation for contrib.postgres.aggregates.StringAggAdd an example for contrib.postgres.aggregates.StringAgg to docs.
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

comment:5 by Mariusz Felisiak, 2 years ago

Triage Stage: AcceptedReady for checkin

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 2 years ago

Resolution: fixed
Status: assignedclosed

In 34459389:

Fixed #34199 -- Added example to StringAgg docs.

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 2 years ago

In 29c9bba8:

[4.1.x] Fixed #34199 -- Added example to StringAgg docs.

Backport of 344593893b6fc5fdda45a74013fc8622401c5058 from main

Note: See TracTickets for help on using tickets.
Back to Top