jsonfield queryset and filtering

Viewed 28

I want to search through the contents of this JSONField:

class Product(models.Model):
    data = models.JSONField()
    number = models.PositiveIntegerField()

class Test(models.Model):
    name = models.CharField("name", max_length = 32, unique = True)
    prod = models.ForeignKey("product", on_delete = models.CASCADE, null = True, blank = True)

where the data looks like this:

{"name": "monkey", "price": 22, "description": "text here", "imgb64": "bkmocdnm32iojo3d0..."}

I want to search via the product_data attributes within the JSONField.

I tried:

Test.objects.filter(product__data__in = "monkey") 

or

Test.objects.filter(product__data__name__icontains = "monkey")

but both result in <QuerySet [].

Additionally, is it possible to search through some of the keys of data? Or even better, exclude some keys when searching?

0 Answers
Related