Replies: 1 comment
-
What worked for me was to override the global import json
from psycopg.types.json import set_json_dumps
from sqlalchemy.dialects.postgresql import ARRAY, JSONB
from sqlmodel import Column, Field, SQLModel
def sqlmodel_json_serializer(obj) -> str:
"""Custom JSON serializer for SQLModel."""
return json.dumps(obj, default=lambda o: o.model_dump())
set_json_dumps(sqlmodel_json_serializer)
class Ingredient(SQLModel):
name: str
quantity: float
class Recipe(SQLModel, table=True):
ingredients: list[Ingredient] = Field(sa_column=Column(ARRAY(JSONB))) However, when reading the data back out again, it's not validated against the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
Is there a way I can use an instance of another model as attribute and have it JSON dumped to/loaded from a Postgres JSONB column?
Operating System
Other
Operating System Details
No response
SQLModel Version
0.0.14
Python Version
3.11.4
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions