Closed lsaint closed 1 year ago
Today I had the same problem. And I found why this happens and how to solve it.
The document ID in Mongo is called _id. The first thought is to name the field "_id: PydanticObjectId" in the projection model. But one underscore in Python marks the field as for internal use. Most likely, pydantic does not use such fields for initialization (for the test, I checked on dataclass and it worked; correct if I'm wrong).
Second thought: aliases. And this is the solution. In your case, you can write the following:
from beanie import Document, PydanticObjectId
from pydantic import BaseModel, Field
class UserView(BaseModel):
id: PydanticObjectId = Field(alias='_id')
username: str
class UserModel(Document):
username: str
email: str
And everything will work!
Perhaps it will later be marked as a bug and fixed, or a better "dirty hack" will be offered.
Today I had the same problem. And I found why this happens and how to solve it.
The document ID in Mongo is called _id. The first thought is to name the field "_id: PydanticObjectId" in the projection model. But one underscore in Python marks the field as for internal use. Most likely, pydantic does not use such fields for initialization (for the test, I checked on dataclass and it worked; correct if I'm wrong).
Second thought: aliases. And this is the solution. In your case, you can write the following:
from beanie import Document, PydanticObjectId from pydantic import BaseModel, Field class UserView(BaseModel): id: PydanticObjectId = Field(alias='_id') username: str class UserModel(Document): username: str email: str
And everything will work!
Perhaps it will later be marked as a bug and fixed, or a better "dirty hack" will be offered.
thank you. it's a good idea for now. In the long run, I prefer this solution #163
This issue is stale because it has been open 30 days with no activity.
This issue was closed because it has been stalled for 14 days with no activity.
got the error:
if i set
id: Optional[PydanticObjectId] = None
got
id = None
in result