Replies: 3 comments
-
You can use alias from And yes, it's pydanatic side. from fastapi import FastAPI
from pydantic import BaseModel, Field
app = FastAPI()
class Item(BaseModel):
name: str
id: str = Field(..., alias="_id")
@app.get("/items/{_id}", response_model=Item)
async def get_item(_id: str):
# Getting the item from DB (fake)
item = {"_id": _id, "name": "test"}
return item |
Beta Was this translation helpful? Give feedback.
0 replies
-
Yep, what @SlyFoxy said ☝️ Thanks for the help @SlyFoxy ! 👏 🍰 |
Beta Was this translation helpful? Give feedback.
0 replies
-
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues. |
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
-
Description
I'm using mongoDB an I want to match the internal id of mongo documents
_id
to a model field, so my model and route looks similar to:So I have notice that the variable
_id
is omitted on parsing from the model so not included in this response.So I was wondering if it is possible to include it somehow?
Checking Pydantic field parsing I found:
https://github.com/samuelcolvin/pydantic/blob/8c0f805d15d6fddca20ec691c6299a92e1ab15c1/pydantic/main.py#L749-L750
So probably is on Pydantic side?
Beta Was this translation helpful? Give feedback.
All reactions