Pydantic as mongo ORM
See original GitHub issueNeed your ideas for a possible Pydantic extension
Hello, although I am new to Python ecosystem, I’ve found that pydantic is really helpful on lots of cases. In fact, I’ve tried to write something similar named Prodict but noticing pydantic, I quit developing it in favor of pydantic. Because it looks like you guys already did most of the work what I wanted to do with Prodict. Thank you this awesome package!
I’ve developed a MongoDB document mapper with pydantic.
It converts pymongo find
and find_one
results to pydantic objects. The usage is almost exactly same, the only difference is if you provide a class derived from BaseModel
, the result is mapped to an object instantiated from that class.
from pydantic import BaseModel
import pymongodantic
class Customer(BaseModel):
_id: Any
name: str
age: int
mongo_client = MongoClient(...).get_database('test')
customers_table = ModeledCollection(database=db_test, name='customers').
customer = customers_table.find_one({'name':'john'}, model=Customer)
print(customer)
# Customer _id = ObjectId("...."), name = 'john', age=25
# It also works for find_one
customers = customers_table.find({}, model=Customer)
for customer in customers:
print(isinstance(customer, Customer)) # True
So my question is:
Would you consider to make pymongodantic
an extension of pydantic
like ujson
?
If so, what should I do?
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (2 by maintainers)
I’ve developed a wrapper around PyMongo and Pydantic that would make this easy, hope somebody finds this useful:
https://ramiawar.github.io/Mongomantic/
@RamiAwar what are these better alternatives that you mentioned. I have been looking for something in this direction. Thanks.