Skip to content
Advertisement

Django model filter targeting JSONField where the keys contain hyphen / dash

I am trying to apply a model filter on a JSONField BUT the keys in the JSON are UUIDs.

So when is do something like…

JavaScript

… I get a compile error. The hyphens in the UUID are the issue.

Any clues if there is an escape char or another behaviour to use? My database is PostgreSQL.

Update 1 – now with added JSON

JavaScript

Advertisement

Answer

That’s looks difficult and I’m not 100% convinced what I have will work.

You can try using the JsonField contains lookup. The lookup is explained more under the docs for HStoreField since it’s shared functionality.

This would look like this:

JavaScript

I think this will allow you to circumvent the fact that the lookups need to be valid Python variable names.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement