add model_dump options to sqlmodel_update#1897
Conversation
|
I can't seem to be able to add labels to this PR. So it'd be great if I can get this reviewed |
YuriiMotov
left a comment
There was a problem hiding this comment.
@A00474880, thanks for your interest and efforts!
I pointed to a couple of issues with this implementation
| obj: builtins.dict[str, Any] | BaseModel, | ||
| *, | ||
| update: builtins.dict[str, Any] | None = None, | ||
| **model_dump_kwargs, |
There was a problem hiding this comment.
kwargs will not give autocompletion, and it's error-prone
| if isinstance(obj, BaseModel): | ||
| obj = obj.model_dump(**model_dump_kwargs) |
There was a problem hiding this comment.
This will break use cases with models that have fields with exclude=True:
from sqlmodel import Field, SQLModel
class Item(SQLModel):
id: str
param: str = Field(exclude=True)
a = Item.model_validate({"id": "1", "param": "1"})
b = Item.model_validate({"id": "1", "param": "2"})
a.sqlmodel_update(b, exclude={"id"})
# a.sqlmodel_update(b)
assert a.param == "2"
.. and probably some other cases when model has settings that change the default serialization schema.
…n settings like `include` and `exclude`
…prove_sqlmodel_update
…prove_sqlmodel_update
|
Hi @YuriiMotov , thank you for the review. My recent commits tries to address your feedback. Please let me know if you need anything else. Change summary:
|
|
Hi all I've had some time to carefully consider the changes and how it affects use cases. With the current implementation of creating a temporary "clean" UpdateModel, some model dump kwargs are effectively ignored when using them directly in the new Effective (8 — work correctly on clean UpdateModel)These don't depend on field-level metadata:
Ignored (3 — clean UpdateModel lacks the corresponding metadata)
Comparison to main branch (getattr approach)The original
Note We can actually add support for fields_def = {
fname: (finfo.annotation, finfo.default)
for fname, finfo in ObjClass.model_fields.items()
}Please let me know if you have any suggestions! |
As per discussion #1838, this PR allows users to use
model_dumpparameters directly insqlmodel_updatefunction.New Usage Pattern
Previous recommend use of
sqlmodel_updateis as follows:This PR allows the following while keeping backward compatibility with the above:
Tests
This PR also adds assertions in the
test_updatefunction that increases test coverage by 2 lines:before:
after: