Django Json field filter throws lookup error

Viewed 1073

I am using django postgres JSONfield and the model structure is as below

from django.contrib.postgres.fields import JSONField

class JsonAnswer(models.Model):
    name = models.CharField(max_length=255)
    data = JSONField(default={})

the data present in the Json field is as below

{
 "owner":{
    "name":"Bob",
    "other_pets":[
      {
       "name":"fishy"
      }
    ]
   },
 "bread":"lab"
}

and my filter query is like this

JsonAnswer.objects.filter(data__owner__name="Bob")

which is throwing the error

FieldError: Unsupported lookup 'owner' for JSONField or join on the field not permitted.

Please explain how to filter the json field data

1 Answers

In your code, above, you have the correct type of JSONField, but the error suggests that the column is not defined as jsonb in the database, for whatever reason (that's what the issue was for me when I encountered a similar error).

Related