ets-labs / python-domain-models

Domain models framework for Python projects
BSD 3-Clause "New" or "Revised" License
22 stars 4 forks source link

DomainModel.get_data() #8

Closed rmk135 closed 8 years ago

rmk135 commented 8 years ago

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

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'}
    ]
}
boonya commented 8 years ago

Issue has been released with version #0.0.4 PR #31