Opened 4 years ago

Closed 4 years ago

#32169 closed New feature (fixed)

Added distinct support for JSONBAgg

Reported by: Chiorufarewerin Owned by: Chiorufarewerin
Component: contrib.postgres Version: dev
Severity: Normal Keywords: jsonbagg
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

I often use JSONBAgg and when there are several aggregates, contains duplicates.

class Author(models.Model):
    name = models.CharField(max_length=50)
    alias = models.CharField(max_length=50, null=True, blank=True)
    age = models.PositiveSmallIntegerField(default=30)


class Article(models.Model):
    authors = models.ManyToManyField(Author, related_name='articles')
    title = models.CharField(max_length=50)


class Product(models.Model):
    article = models.ForeignKey(Article, related_name='products', on_delete=models.CASCADE)
    name = models.CharField(max_length=255)


Article.objects.annotate(
     authors_json=ArrayAgg(
         Func(
             Value('name'), 'authors__name',
             Value('alias'), 'authors__alias',
             function='jsonb_build_object'
         ),
         distinct=True,
     ),
     products_json=ArrayAgg(
         Func(
             Value('name'), 'products__name',
             function='jsonb_build_object'
         ),
         distinct=True,
     ),
)

Change History (3)

comment:1 by Mariusz Felisiak, 4 years ago

Owner: set to Chiorufarewerin
Status: newassigned
Triage Stage: UnreviewedAccepted

comment:2 by Mariusz Felisiak, 4 years ago

Triage Stage: AcceptedReady for checkin

comment:3 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 18c8ced8:

Fixed #32169 -- Added distinct support to JSONBAgg.

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