Declaring a JSON attribute

For declaring a JSON attribute with Pony you should use the Json type. This type can imported from pony.orm package:

  1. from pony.orm import *
  2. db = Database()
  3. class Product(db.Entity):
  4. id = PrimaryKey(int, auto=True)
  5. name = Required(str)
  6. info = Required(Json)
  7. tags = Optional(Json)
  8. db.bind('sqlite', ':memory:', create_db=True)
  9. db.generate_mapping(create_tables=True)

The info attribute in the Product entity is declared as Json. This allows us to have different JSON structures for different product types, and make queries to this data.