""" Extensions ========== This example shows howw to extend a model by providing an extension to it. The extension will add a new attribute to the parent object and will get its content from whatever the extension returns. Data can be thus fetched from: * A different datastore. * A different microservice. * ... and it's pure python. So you can fetch it from anywhere. """ import json from sukimu import schema from sukimu import response Project = schema.Schema(schema.Table(None)) @Project.extension('subaccount') def extension_subaccount(item, fields): """Get the subaccount information for a project. """ if item.get('subaccount_id') == 2: return {'subaccount_name': 'Michael'} return {'subaccount_name': 'John'} # Example dataset projects = response.create_success_response([{ 'project_id': '1', 'subaccount_id': '2' }, { 'project_id': '2', 'subaccount_id': '3' }]) Project.decorate_response(projects, fields=['subaccount']) print(json.dumps(projects.message))