# Example usage of database funtions(`Sum` and `Avg`) and Django `JSONField`'s `jsonb_array_length` # PostgreSQL json field functions: https://www.postgresql.org/docs/9.5/static/functions-json.html # Django `F` and `Func`: https://docs.djangoproject.com/en/1.10/ref/models/expressions/#f-expressions # Django aggregation: https://docs.djangoproject.com/en/1.10/topics/db/aggregation/ # Django `JSONField`: https://docs.djangoproject.com/en/1.9/ref/contrib/postgres/fields/#jsonfield # Given a model class Contact(models.Model): numbers = JSONField(help_text="An array of numbers") from django.db.models import Sum, Avg, F, Func # Get the length of `numbers` field for one record contact = Contact.objects.annotate(numbers_len=Func(F('numbers'), function='jsonb_array_length')).latest('pk') >>> contact.numbers_len 3 # Get the sum of the length of all records >>> contact = Contact.objects.annotate(numbers_len=Func(F('numbers'), function='jsonb_array_length')).aggregate(Sum('numbers_len')) >>> contact {'numbers_len__sum': 247485107} # 247MM! # Get the average length of all `numbers` >>> contact = Contact.objects.annotate(numbers_len=Func(F('numbers'), function='jsonb_array_length')).aggregate(Avg('numbers_len')) >>> contact {'numbers_len__avg': Decimal('1.9999268527384372')}