0

I have made Django project with the database on PostgreSQL. I have one column data which is a JSONField() object. Now for querying in the data JSON blob, the docs show only one way. Say some rows have the key as 'Salary' in data so according to docs if I want to retrieve the row with salary as something then the way is:

TableName.objects.filter(data__Salary__gte = 15000)

Now the problem is that I want to make it user specific like the user will tell the key and pass it to the function and I want something like this:

keyValue = 'Salary'
TableName.objects.filter(data__keyValue__gte = 15000)

But this does not work for obvious reasons as there is no column of keyValue in data. Is there any other way of doing it?

Adit Modi
  • 1
  • 3

1 Answers1

2

Try this

keyValue = 'Salary'

params = {'data__%s__gte' % keyValue:15000}
TableName.objects.filter(**params)
Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46