1

I have model with JSONField, and some of those values in this field can be lists. when I'm search after all the values how bigger than some number, 3 for example. the query returns all the lists even though the values in those lists are smaller, how can i get only the correct rows?

query: Books.objects.filter(pages__t__gte=2):

returns all the books with t>=2 and all the books with list as t, for example: t:[0,1]

N.Bar
  • 135
  • 1
  • 1
  • 10
  • Take a look at https://stackoverflow.com/questions/10445176/how-to-write-a-query-to-get-find-value-in-a-json-field-in-django – Nannan AV May 14 '18 at 09:59
  • already saw it, the problem is with lists – N.Bar May 14 '18 at 10:08
  • 1
    From a quick search (that you could have done yourself), it looks like what you're asking for is not possible (or at least not without going thru all sort of stored procedure etc stuff). Note that you wouldn't have this issue with a properly normalized schema... – bruno desthuilliers May 14 '18 at 10:30
  • It is possible to do with a custom postgres function. It's not built in to django querysets, unfortunately. Postgres does support python scripting, if you are willing to experiment with that. There's not a bunch of examples available in the docs, but it is possible. https://www.postgresql.org/docs/10/static/plpython.html – Håken Lid May 14 '18 at 10:36

0 Answers0