First commit to the code repository
This commit is contained in:
1 parent
692cdee407
commit
0c0dd1cb56
323 files changed
+45712
No files matched your search
@@ -0,0 +1,29 @@
|
||||
class SqlalchemyUtils:
|
||||
|
||||
@staticmethod
|
||||
def model_to_dict(model):
|
||||
return {column.name: getattr(model, column.name) for column in model.__table__.columns}
|
||||
|
||||
@staticmethod
|
||||
def dict_to_model(model_class, data_dict):
|
||||
"""
|
||||
Convert dictionary to SQLAlchemy model instance
|
||||
|
||||
Args:
|
||||
model_class: SQLAlchemy model class
|
||||
data_dict: Dictionary containing model data
|
||||
|
||||
Returns:
|
||||
Instance of the model class with data populated
|
||||
"""
|
||||
instance = model_class()
|
||||
|
||||
# Get all column names for the model
|
||||
column_names = {column.name for column in model_class.__table__.columns}
|
||||
|
||||
# Set attributes that exist in both the dictionary and the model
|
||||
for key, value in data_dict.items():
|
||||
if key in column_names and hasattr(instance, key):
|
||||
setattr(instance, key, value)
|
||||
|
||||
return instance
|
||||
Reference in new issue
Block a user