-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for MongoDB ObjectID in Pydantic Models #133
Comments
@SkandaPrasad-S if you're using pydantic, FastAPI and pymongo, you can validate the Anyway, I agree with you that ideally setting arbitrary_types_allowed should not be needed and there should be a standard validator. |
I think this is a valuable feature request, but belongs on the I might also suggest using |
I'd also be open to closing this issue - seems like a specific enough request that corresponds with |
I would love to have this in pydnatic instead of beanie. I understand that it probably might make sense @sydney-runkle, but I would rather have it in pydantic as one of the inbuilt types, especially with the increase in FARM stack usage.
I would rather not have this in every single MongoDB app that I make with FastAPI. |
Also
But I ended up with the below error
|
@SkandaPrasad-S I agree with you that having a custom type for Mongo ObjectId would be a good addition. Anyway, there are some issues with the snippet you provided:
If you're working with pydantic v2, try this instead: from bson import ObjectId
from pydantic import BaseModel, Field, ConfigDict
class SingleGoal(BaseModel):
"""Goal Record Model with ID."""
model_config = ConfigDict(arbitrary_types_allowed=True)
id: ObjectId = Field(
alias="_id",
description="Unique identifier in mongo db"
) I found this "workaround" to work very well in projects where you use pydantic and pymongo, but not beanie. |
Ahh interesting, I see why it did not work @Ale-Cas. Thank you so much, makes more sense now. I am going to raise the PR myself if that would not be a problem for this type if that is okay! @sydney-runkle ? |
Created a PR At #151 |
This is a beautiful piece of work. Thank you @SkandaPrasad-S |
You can use the
You can perform basic operations such as generating, validating, and converting ObjectIds:
You can install the package: |
* Add support for pymongo bson ObjectId (#133) * Fix py38 type hint * Add test for json schema
Initial Checks
Description
I am using Pydantic in conjunction with FastAPI and MongoDB, and I would like to request support for MongoDB ObjectID as one of the field types in Pydantic models.
Background:
Currently, Pydantic supports various field types, but there isn't a specific type for MongoDB ObjectID. MongoDB ObjectID is commonly used in FastAPI applications with MongoDB databases, and having native support for it in Pydantic would greatly enhance the validation capabilities.
Suggested Solution:
I propose adding a dedicated type for MongoDB ObjectID in Pydantic, similar to how EmailStr and other specialized types work. This could be achieved by introducing a type like MongoObjectId or extending the existing PyObjectId to handle MongoDB ObjectID validation seamlessly.
Additional Context:
MongoDB ObjectID validation is a common requirement in FastAPI applications, especially when dealing with MongoDB databases. Adding native support in Pydantic would simplify the code and improve the developer experience.
I would love it if we could do something like the below with FASTAPI that works seamlessly in generating the OpenAPI schema as well.
This is because all the workarounds for the above issue face OpenAPI schema issues.
Affected Components
.model_dump()
and.model_dump_json()
model_construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: