Assigning value to the JSON attribute
Usually, a JSON structure contains a combination of dictionaries and lists containing simple types, such as numbers, strings and boolean. Let’s create a couple of objects with a simple JSON structure:
p1 = Product(name='Samsung Galaxy S7 edge',
info={
'display': {
'size': 5.5,
},
'battery': 3600,
'3.5mm jack': True,
'SD card slot': True,
'colors': ['Black', 'Grey', 'Gold'],
},
tags=['Smartphone', 'Samsung'])
p2 = Product(name='iPhone 6s',
info={
'display': {
'size': 4.7,
'resolution': [750, 1334],
'multi-touch': True,
},
'battery': 1810,
'3.5mm jack': True,
'colors': ['Silver', 'Gold', 'Space Gray', 'Rose Gold'],
},
tags=['Smartphone', 'Apple', 'Retina'])
In Python code a JSON structure is represented with the help of the standard Python dict and list. In our example, the info
attribute is assigned with a dict. The tags
attribute keeps a list of tags. These attributes will be serialized to JSON and stored in the database on commit.