X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;ds=inline;f=plomtask%2Fdb.py;h=1cecc16f6985b555f25757ad6e9f65724311a287;hb=014e59d97c912cff4ae2922c1d9284c086694cf1;hp=99998a6ab29f760ba0d62f90395739dad4b521ff;hpb=c5449a0b00f8865b1129ed56bdd16f1cc055bc87;p=plomtask
diff --git a/plomtask/db.py b/plomtask/db.py
index 99998a6..1cecc16 100644
--- a/plomtask/db.py
+++ b/plomtask/db.py
@@ -271,6 +271,23 @@ class BaseModel(Generic[BaseModelId]):
assert isinstance(other.id_, int)
return self.id_ < other.id_
+ @property
+ def as_dict(self) -> dict[str, object]:
+ """Return self as (json.dumps-coompatible) dict."""
+ d: dict[str, object] = {'id': self.id_}
+ for k in self.to_save:
+ attr = getattr(self, k)
+ if hasattr(attr, 'as_dict'):
+ d[k] = attr.as_dict
+ d[k] = attr
+ for k in self.to_save_versioned:
+ attr = getattr(self, k)
+ d[k] = attr.as_dict
+ for r in self.to_save_relations:
+ attr_name = r[2]
+ d[attr_name] = [x.as_dict for x in getattr(self, attr_name)]
+ return d
+
# cache management
# (we primarily use the cache to ensure we work on the same object in
# memory no matter where and how we retrieve it, e.g. we don't want