Skip to content Skip to sidebar Skip to footer

Django - Unsupported Lookup For Jsonfield Or Join On The Field Not Permitted

I am having a Json field in my models as - class Product(models.Model): ... detailed_stock = JSONField(load_kwargs={'object_pairs_hook': collections.OrderedDict}

Solution 1:

I think you are using django-jsonfield as indicated by load_kwargs={'object_pairs_hook': collections.OrderedDict} instead of django.contrib.postgres.fields.JSONField.

django-jsonfield is for databases which don't offer a native dict type and is based on a simple TextField. When you access the field value using product.detail_stock the internally saved str is converted to dict using json.loads() by the field itself. Hence you can only use operations like icontains and contains for querying that field.

If you are using postgres as a database, you are able to take full advantage of django.contrib.postgres.fields.JSONField as the documentation states. But you have to import the correct JSONfield by using django.contrib.postgres.fields import JSONField.

There is a solution for mysql (package django-mysql) too.

Solution 2:

If you are using Django and have used:

from json_field import JSONField

Then you can not benefit the SQL lookup. As mentioned above, JSONField overrides the TextField only. It validates the JSONformat and dumps as string.

classJSONField(models.TextField):
    """ Stores and loads valid JSON objects. """

Instead use

from django.db import models
data = models.JSONField(null=True)

Post a Comment for "Django - Unsupported Lookup For Jsonfield Or Join On The Field Not Permitted"