| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
This workaround fixes Pydantic serialization/deserialization while not modifying the internal alias logic. I cannot figure out why alias is not being passed through anymore. For example, I was needing to add schema_extra with these key values in order to read data from a Json with different field names. Thank you for your consideration.
There was a problem hiding this comment.
LGTM. I tested it on a SQLModel and it works fine. It keeps the syntax clean by using the same alias field.
Sorry, something went wrong.
|
Hi @STocidlowski, thanks for the contribution! Do you have a minimal reproducible example that shows the bug on main (but has it fixed with this PR)? |
Sorry, something went wrong.
|
Thanks for this useful fix. It work for my usecase : Reproductible example : from sqlmodel import Field, Session, SQLModel, create_engine, select
class Hero(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
name: str
secret_name: str = Field(..., alias="myLittleSecretAlias") # Using alias for JSON serialization
age: int | None = None
sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"
engine = create_engine(sqlite_url, echo=True)
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
hero_1 = Hero(name="Deadpond", myLittleSecretAlias="Dive Wilson")
session.add(hero_1)
statement = select(Hero).where(Hero.name == "Deadpond")
hero = session.exec(statement).one_or_none()
# without the fix the result is :
# {'name': 'Deadpond', 'secret_name': 'Dive Wilson', 'id': 1, 'age': None}
# with the fix it is :
# {'name': 'Deadpond', 'myLittleSecretAlias': 'Dive Wilson', 'id': 1, 'age': None}
print(hero.model_dump(by_alias=True))
|
Sorry, something went wrong.
|
a workaround is the usage of schema_extra={"serialization_alias": "metadata"} instead of the "alias" parameter. |
Sorry, something went wrong.
|
Thanks @STocidlowski, @jrealpe and @gillespilloudkerlink for your contributions here. I'll go ahead and close this PR in favor of #1577 which is more extensive. But thanks again! 馃檹 |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
bug
/This workaround fixes Pydantic serialization/deserialization while not modifying the internal alias logic. I cannot figure out why alias is not being passed through anymore.
For example, I was needing to add schema_extra with these key values in order to read/write data from a Json
https://docs.pydantic.dev/latest/concepts/fields/#field-aliases
#290
Thank you for your consideration.