Question

I'm trying to create a custom filter and search in a Django list view that would use the values stored within a JSONField, just as if they were defined as regular model fields.

I think I need to define a model Manager with a custom get_queryset() function, but I'm not sure how to "dehydrate" the json data and return it as part of the queryset.

Some other approaches have failed so far: I can insert values to the context dict in a custom ListView, but that context cannot be queried. Also tried defining attributes in admin.py and wrapping some @property definitions in models.py; encountering the same problem here because the properties cannot be queried.

Any suggestions?

Was it helpful?

Solution

You can't do that (in a normal RDBMS, anyway).

Filters are evaluated by the database, which knows nothing of the fields within the JSON: it's just an opaque blob. If you need to search on those fields, you'll need to store them as proper db-accessible data.

OTHER TIPS

If (and only if) you are using PostgreSQL you can do that

Take a look at how to query JSONField in PostgreSQL in Django docs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top