I'm using Django 1.9 and Postgres 9.5. I have a model (MyModel
) with a JSONField
(django.contrib.postgres.fields
). The JSON value has the same structure in all the objects in the model.
{
"key1": int_val_1,
"key2": int_val_2
}
I want to order my queryset by a value of a specified key in my JSONField
. Something like
MyModel.objects.annotate(val=F('jsonfield__key1')).order_by('val')
This does not work - i get the following error
Cannot resolve keyword 'key1' into field. Join on 'jsonfield' not permitted.
Is there anyway i can achieve this?