Better understanding create_pydantic_model with foreign keys
#1296
Replies: 3 comments 1 reply
-
|
@badlydrawnrob If you look at the source code of create_pydantic_model, if you want to use FK, you have to use # nested Pydantic model
FruitsModelOut: Any = create_pydantic_model(
table=Fruits,
model_name="FruitsModelOut",
nested=True,
)
# using objects query
@app.get("/", response_model=List[FruitsModelOut])
async def retrieve_all_fruits():
fruits = await Fruits.objects(Fruits.all_related())
return fruits
# using select query
@app.get("/", response_model=List[FruitsModelOut])
async def retrieve_all_fruits():
fruits = await Fruits.select(
Fruits.all_columns(exclude=[Fruits.id, Fruits.color]),
Fruits.color.all_columns(),
).output(nested=True)
return fruitsThe result is nicely nested JSON response [
{
"color": {
"color": "#cc0f35",
"background": "#feecf0",
"name": "red"
},
"image": null,
"name": "Banana",
"url": "ece2894a-be27-4723-ba5a-ee5d5a163f82"
}
]If you want, as you said, a from pydantic import BaseModel, UUID4
class FruitsModelOut(BaseModel):
name: str
image: str | None = None
url: UUID4
# flatten Colors FK
color_color: str
color_background: str
color_name: str
@app.get("/", response_model=List[FruitsModelOut])
async def retrieve_all_fruits():
fruits = await Fruits.select(
Fruits.image,
Fruits.name,
Fruits.url,
Fruits.color.color.as_alias("color_color"),
Fruits.color.background.as_alias("color_background"),
Fruits.color.name.as_alias("color_name"),
)
return fruitsand result then is [
{
"name": "Banana",
"image": null,
"url": "ece2894a-be27-4723-ba5a-ee5d5a163f82",
"color_color": "#cc0f35",
"color_background": "#feecf0",
"color_name": "red"
}
] |
Beta Was this translation helpful? Give feedback.
-
|
Ok so right now I'm happy to write my own Pydantic models: Elm Lang prefers a flatter model, but uses decoders so can consume almost any
It might simplify the API to pick one shape and standardise it. |
Beta Was this translation helpful? Give feedback.
-
|
@sinisaos You've misunderstood me. As you've said before, All you need to know about Elm is that it's types and return values are much stricter, so they tend to be more predictable. Anyway, for now I'm happy with a flat style and will create my own models like your example above, thanks. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
So I'm having a little trouble understanding
create_pydantic_modelwhere foreign keys are concerned — I have a feeling it might be easier (in some cases) to just create your own Pydantic models — perhaps the documentation could be a little more clear and extensive in this area? I imagined the automatic Pydantic models are flexible enough for most queries, but can't figure out how to do it reliably.See example code here, I've tried:
Flat shape
I'm happy with a flat shape, but how do you
create_pydantic_modelfor it? I think the way I've been trying it is wrong.Without any pydantic model
Output without a
response_modelpydantic type (a list of fruits):[ { "image": null, "name": "Banana", "url": "4352d224-b0ce-4639-8696-942b2220fc0a", "color.id": 1, "color.color": "#cc0f35", "color.background": "#feecf0", "color.name": "red" } ]What I've tried to do (flat shape)
My first thought was to
include_columns=(Fruits.name, Fruits.color, Fruits.color.name, ...)but it seems onlyFruitscolumns are allowed there (not the foreign table fields).I've also tried a plain model with basic arguments (single result example)
Outputs this, with none of the extra fields I'd expect (with
coloralso set tonull)Nested shape
This feels a little easier to understand, but I'd prefer a flatter model ...
Response (unfortunately it gives
nullvalue forid, but you can remove theinclude_default_columnsargument to hide it)[ { "id": null, "color": { "id": 1, "color": "#cc0f35", "background": "#feecf0", "name": "red" }, "image": null, "name": "Banana", "url": "4352d224-b0ce-4639-8696-942b2220fc0a" } ]TL;DR
So yeah, using "magic" at the moment feels a little harder to understand than just creating your own Pydantic models for foreign key fields with a flat shape, or for more complex queries. Perhaps this is the limitation of
create_pydantic_types? Or I'm misunderstanding.Beta Was this translation helpful? Give feedback.
All reactions