From Getting Started with MongoDB and FastAPI | MongoDB, this pydantic model has id
field with alias _id
.
- What is the significance of this alias?
- Must
allow_population_by_field_name = True
be added for this alias to be used to intended effect or having default False works too?
class StudentModel(BaseModel):
id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
name: str = Field(...)
email: EmailStr = Field(...)
course: str = Field(...)
gpa: float = Field(..., le=4.0)
class Config:
allow_population_by_field_name = True
arbitrary_types_allowed = True
json_encoders = {ObjectId: str}
schema_extra = {
"example": {
"name": "Jane Doe",
"email": "jdoe@example.com",
"course": "Experiments, Science, and Fashion in Nanophotonics",
"gpa": "3.0",
}
}
Can someone more clearly explain the datatypes at each stage of the request/response data transformation pipeline? (Pydantic(fastapi) ↔ pymongo driver ↔ mongodb )
I’m not sure which stage expects/produces id
and which stage expects/produces _id
, and why the api should accept both id and _id.
I’m also confused about 3 cases when is id designed to be
- automatically generated by mongodb
- generated by driver and passed into mongodb
- generated by pydantic and passed to driver to mongodb.
which the article wasn’t clear at explaining during the alias section.
That article says We set this id value automatically to an ObjectId string, so you do not need to supply it when creating a new student.
Does this mean the alias of _id
only comes into play when someone calls fastapi endpoint and provided an _id in body of post request, so it doesn’t have to be automatically created by pydantic?
Is case 1 and 2 above even relevant when working with fastapi (when users interact only with the api)?
The article says “when creating a new student”. What happens if I had already inserted the data directly through mongo shell or driver (meaning not going through pydantic), and I only use fastapi to read data, does this mean this whole id aliasing thing can be ignored?
Furthermore, does it also mean I don’t even have to make any pydantic models if i only want to write a read-only fastapi?