Modifying JSON attribute

For modifying the JSON attribute value, you use the standard Python list and dict API as well:

  1. >>> Product[1].info['colors'].append('Silver')
  2. >>> Product[1].info['colors']
  3. ['Black', 'Grey', 'Gold', 'Silver']

Now, on commit, the changes will be stored in the database. In order to track the changes made in the JSON structure, Pony uses its own dict and list implementations which inherit from the standard Python dict and list.

Below is a couple more examples of how you can modify the the JSON value.

  1. p = Product[1]
  2. # assigning a new value
  3. p.info['display']['size'] = 4.7
  4. # popping a dict value
  5. display_size = p.info['display'].pop('size')
  6. # removing a dict key using del
  7. del p.info['display']
  8. # adding a dict key
  9. p.info['display']['resolution'] = [1440, 2560]
  10. # removing a list item
  11. del p.info['colors'][0]
  12. # replacing a list item
  13. p.info['colors'][1] = ['White']
  14. # replacing a number of list items
  15. p.info['colors'][1:] = ['White']

All of the actions above are regular Python operations with attributes, lists and dicts.