xxxxxxxxxx
from django.db import models
class Patient(models.Model):
name = models.CharField(max_length=256)
data = models.JSONField()
# Create a patient instance
Patient(name='John Doe', data={
'address': '123 Some House Number',
'city': 'Some City',
'state': 'Utah',
})
# Filter and delete the patient
Patient.objects.filter(patient='John Doe', data__state='Utah')
xxxxxxxxxx
# New version
class MyModel(models.Model):
attribute = models.JSONField( )
# Old version (<3.1, Postgres only)
from django.contrib.postgres.fields import JSONField
class MyModel(models.Model):
attribute = JSONField( )
xxxxxxxxxx
>>> Dog.objects.create(name='Rufus', data={
'breed': 'labrador',
'owner': {
'name': 'Bob',
'other_pets': [{
'name': 'Fishy',
}],
},
})
>>> Dog.objects.create(name='Meg', data={'breed': 'collie', 'owner': None})
>>> Dog.objects.filter(data__breed='collie')
<QuerySet [<Dog: Meg>]>