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?

1
  • possibly duplicate of this. Commented Jan 18, 2018 at 12:07

1 Answer 1

2

You can create a dictionary and unpack it in the filter call using the ** operator.

>>> key_value = 'Salary'
>>>
>>> params = {'data__%s__gte' % key_value:15000}
>>> TableName.objects.filter(**params)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.