Skip to content

Commit ff4c72a

Browse files
committed
entity: allow members init from constructor objectbox#24
1 parent 5df92a4 commit ff4c72a

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

objectbox/model/entity.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,14 @@ def __init__(self, cls, id: int, uid: int):
4747
self.id_property = None
4848
self.fill_properties()
4949

50-
def __call__(self, *args):
51-
return self.cls(*args)
50+
def __call__(self, **properties):
51+
""" The constructor of the user Entity class. """
52+
object_ = self.cls()
53+
for prop_name, prop_val in properties.items():
54+
if not hasattr(object_, prop_name):
55+
raise Exception(f"Entity {self.name} has no property \"{prop_name}\"")
56+
setattr(object_, prop_name, prop_val)
57+
return object_
5258

5359
def fill_properties(self):
5460
# TODO allow subclassing and support entities with __slots__ defined
@@ -235,8 +241,8 @@ def unmarshal(self, data: bytes):
235241
return obj
236242

237243

238-
def Entity(id: int = 0, uid: int = 0):
244+
def Entity(id: int = 0, uid: int = 0) -> Callable[[Type], _Entity]:
239245
""" Entity decorator that wraps _Entity to allow @Entity(id=, uid=); i.e. no class arguments. """
240-
def wrapper(cls):
241-
return _Entity(cls, id, uid)
246+
def wrapper(class_):
247+
return _Entity(class_, id, uid)
242248
return wrapper

0 commit comments

Comments
 (0)