#32169: Added distinct support for JSONBAgg
--------------------------------------------+--------------------------
               Reporter:  Chiorufarewerin   |          Owner:  (none)
                   Type:  New feature       |         Status:  new
              Component:  contrib.postgres  |        Version:  master
               Severity:  Normal            |       Keywords:  jsonbagg
           Triage Stage:  Unreviewed        |      Has patch:  1
    Needs documentation:  0                 |    Needs tests:  0
Patch needs improvement:  0                 |  Easy pickings:  0
                  UI/UX:  0                 |
--------------------------------------------+--------------------------
 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,
      ),
 )
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32169>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/058.40f6cb25ef188aaf5f4e919b23395412%40djangoproject.com.

Reply via email to