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.aggregate(min=Min('attrs__my_key'))
Also, it would be useful to extract specific keys:
Model.objects.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?
Notes:
- I know how to make a plain Postgres query to do the job, but am searching specifically for an ORM solution to have the ability to filter etc.
- I suppose this can be done with a (relatively) new query expressions/lookups API, but I haven't studied it yet.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…