-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Closed
Labels
@haykkh
Description
I'm working through a project that was (at one point a long time ago) based on this stack.
I've run into an issue with SQLAlchemy hybrid_properties not being included when jsonable_encoder
is called on db_obj
. This got me thinking as to why we even encode db_obj
into obj_data
here, seeing as we only use obj_data
to iterate through the attributes of db_obj
.
To fix this issue I've removed the jsonable_encoder
logic here, and instead check if db_obj
contains the attribute with hasattr
(see below). Am I missing something obvious as to why I should be using jsonable_encoder
here? Or are my modifications safe?
My changes:
def update( self, db: Session, *, db_obj: ModelType, obj_in: Union[UpdateSchemaType, Dict[str, Any]] ) -> ModelType: if isinstance(obj_in, dict): update_data = obj_in else: update_data = obj_in.dict(exclude_unset=True) for field in update_data: if hasattr(db_obj, field): setattr(db_obj, field, update_data[field]) db.add(db_obj) db.commit() db.refresh(db_obj) return db_obj