summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
Diffstat (limited to 'docs')
-rw-r--r--docs/ref/contrib/postgres/aggregates.txt26
1 files changed, 25 insertions, 1 deletions
diff --git a/docs/ref/contrib/postgres/aggregates.txt b/docs/ref/contrib/postgres/aggregates.txt
index 6d85d5c478..9223f17ab8 100644
--- a/docs/ref/contrib/postgres/aggregates.txt
+++ b/docs/ref/contrib/postgres/aggregates.txt
@@ -125,7 +125,8 @@ General-purpose aggregation functions
.. class:: JSONBAgg(expressions, distinct=False, filter=None, default=None, ordering=(), **extra)
Returns the input values as a ``JSON`` array, or ``default`` if there are
- no values.
+ no values. You can query the result using :lookup:`key and index lookups
+ <jsonfield.key>`.
.. attribute:: distinct
@@ -145,6 +146,29 @@ General-purpose aggregation functions
Examples are the same as for :attr:`ArrayAgg.ordering`.
+ Usage example::
+
+ class Room(models.Model):
+ number = models.IntegerField(unique=True)
+
+ class HotelReservation(model.Model):
+ room = models.ForeignKey('Room', on_delete=models.CASCADE)
+ start = models.DateTimeField()
+ end = models.DateTimeField()
+ requirements = models.JSONField(blank=True, null=True)
+
+ >>> from django.contrib.postgres.aggregates import JSONBAgg
+ >>> Room.objects.annotate(
+ ... requirements=JSONBAgg(
+ ... 'hotelreservation__requirements',
+ ... ordering='-hotelreservation__start',
+ ... )
+ ... ).filter(requirements__0__sea_view=True).values('number', 'requirements')
+ <QuerySet [{'number': 102, 'requirements': [
+ {'parking': False, 'sea_view': True, 'double_bed': False},
+ {'parking': True, 'double_bed': True}
+ ]}]>
+
.. deprecated:: 4.0
If there are no rows and ``default`` is not provided, ``JSONBAgg``