[Django]-Postgres 9.4 Django 1.9 get all json keys

9👍

You can use Func() expression (docs) of django to use Postgres function jsonb_object_keys(docs)

class JsonKeys(Func):
    function = 'jsonb_object_keys'

Now you can write

File.objects.all().annotate(metadata_keys=JsonKeys('metadata')

3👍

I’ll suggest the following

list_of_keys = []
for obj in File.objects.all().values_list('metadata', flat=True)
   list_of_keys = list_of_keys + obj.keys()

Now you will have a list of keys, then you can do

set = {}
map(set.__setitem__, list_of_keys, [])
unique_keys = set.keys()

Leave a comment