How to aggregate (min/max etc.) over Django JSONField data?

145 views
Skip to first unread message

a1tus

unread,
Dec 18, 2015, 7:43:03 AM12/18/15
to Django users
I'm using django 1.9 with its built-in JSONField and postgres 9.4. In my model's attrs json field I store objects with some values, including numbers. And I need to aggregate over them to find min/max values. Something like this:

Model.objects.filter(**some).aggregate(min=Min('attrs__my_key'))

Also it would be useful to extract specific keys:

Model.objects.filter(**some).values_list('attrs__my_key', flat=True)

The above queries fail with FieldError: "Cannot resolve keyword 'my_key' into field. Join on 'attrs' not permitted."

Is it possible somehow?
I've started to dig into query expressions but without any success yet.
Reply all
Reply to author
Forward
0 new messages