Open angru opened 4 years ago
Exclude hook example
import typing as t
from corm import Storage, Entity, Nested, DumpHook
storage = Storage()
class Address(Entity):
id: int
street: str
number: int
class User(Entity):
id: int
name: str
address: Address = Nested(entity_type=Address)
class ExcludeHook(DumpHook):
match_entities = [User, Address]
def __init__(self, exclude_fields: t.List[str], match_entities: t.List[t.Type[Entity]]):
super().__init__(match_entities)
self.exclude_fields = exclude_fields
def match(self, data, entity: Entity):
for field in self.exclude_fields:
data.pop(field, None)
return data
john = User({'id': 1, 'name': 'John', 'address': {'id': 2, 'street': 'First', 'number': 1}}, storage)
assert john.dict(hooks=[ExcludeHook(exclude_fields=['id'])]) == {
'name': 'John',
'address': {'street': 'First', 'number': 1},
}
Something that able to change data during create entity or make dict from entity