ets-labs/python-domain-models

DomainModel.get_data()

rmk135 opened this issue · 1 comments

The idea is to create model.get_data() method that will return model's data as a dict.

  • In case, when model has relations with other models (fields.Model or fields.Collection), the data of related models should be returned also.

Note: currently there is model.__data__ derived attribute that should be dropped in favor of model.get_data() method.

Example:

class Photo(models.DomainModel):

    id = fields.Int()
    storage_path = fields.String()


class Profile(models.DomainModel):

    id = fields.Int()
    name = fields.String()
    main_photo = fields.Model(Photo)
    photos = fields.Collection(Photo)


photo1 = Photo(id=1, storage_path='some/dir/where/photos/live/1.jpg')
photo2 = Photo(id=2, storage_path='some/dir/where/photos/live/2.jpg')

profile = Profile(id=1, name='John', main_photo=photo1, photos=[photo1, photo2])

assert profile.get_data() == {
    'id': 1,
    'name': 'John',
    'main_photo': {'id': 1, 'storage_path': 'some/dir/where/photos/live/1.jpg'},
    'photos': [
        {'id': 1, 'storage_path': 'some/dir/where/photos/live/1.jpg'},
        {'id': 2, 'storage_path': 'some/dir/where/photos/live/2.jpg'}
    ]
}

Issue has been released with version #0.0.4 PR #31