1

I'm using Django 3.2.3 / Python 3.7.8 / PostgreSQL 12.3

models.py

class Booking(models.Model):
    reference = models.CharField(max_length=15, unique=True)
    services = models.JSONField()

The services structure is:

{
'1':{
    'id': 3,
    'name': 'Name 1'
    },
'2':{
    'id': 4,
    'name': 'Name 2
    },
'3':{
    'id': 3,
    'name': 'Name 3
    },
 ...
}

How to filter the Booking with services having id 3?

I tried Booking.objects.filter(services__contains=[{'id': 3}]) but couldn't get through.

Kindly help.

Rk..
  • 753
  • 1
  • 10
  • 27

1 Answers1

0

I think you should try is_null look up.

Rather than contains, as follow:

Booking.objects.filter(services__3__isnull=False)

Also I recommend you to see the django JSON queryset documentation here

xtornasol512
  • 2,399
  • 1
  • 12
  • 7