0

I am using django 3.2 with Postgres as DB. I have a model with JSONField:

class MyModel(models.Model):
    data = models.JSONField(default=dict, blank=True)

In database there are a lot of records in this table and some data have JSON values as object and others as lists:

{
    "0:00:00 A": "text",
    "0:01:00 B": "text",
    "0:02:00 C": "text",
}


[
    {"time": "0:00:00", "type": "A", "description": "text"},
    {"time": "0:01:00", "type": "B", "description": "text"},
    {"time": "0:02:00", "type": "C", "description": "text"},
]

I need to filter all records which has JSON values as objects.

What I tried is to use has_key with time frame "0:00:00" :

result = MyModel.objects.filter(data__has_key="0:00:00 A")

But I really cant use it because I am not sure what the key with time frame look like completely.

Any ideas how to filter JSONField values by object struct?

belek
  • 907
  • 9
  • 35

0 Answers0