1# New version
2class MyModel(models.Model):
3 attribute = models.JSONField(...)
4
5
6
7# Old version (<3.1, Postgres only)
8from django.contrib.postgres.fields import JSONField
9
10class MyModel(models.Model):
11 attribute = JSONField(...)
1>>> Dog.objects.create(name='Rufus', data={
2... 'breed': 'labrador',
3... 'owner': {
4... 'name': 'Bob',
5... 'other_pets': [{
6... 'name': 'Fishy',
7... }],
8... },
9... })
10>>> Dog.objects.create(name='Meg', data={'breed': 'collie', 'owner': None})
11
12>>> Dog.objects.filter(data__breed='collie')
13<QuerySet [<Dog: Meg>]>
14