From 670442f9eaef269d05a2f4c235ca0fa8f4bff414 Mon Sep 17 00:00:00 2001 From: BryantHe Date: Sat, 10 Jun 2023 15:24:42 +0800 Subject: [PATCH] add chassis utils --- chassis/utils.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 chassis/utils.py diff --git a/chassis/utils.py b/chassis/utils.py new file mode 100644 index 0000000..5e3d22a --- /dev/null +++ b/chassis/utils.py @@ -0,0 +1,14 @@ +from datetime import datetime +from sqlalchemy.orm import class_mapper + + +def model_to_dict(model): + if not model: + return + model_dict = {} + for key, column in class_mapper(model.__class__).c.items(): + if isinstance(getattr(model, key), datetime): + model_dict[column.name] = str(getattr(model, key)) + else: + model_dict[column.name] = getattr(model, key, None) + return model_dict \ No newline at end of file