question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pydantic as mongo ORM

See original GitHub issue

Need 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:closed
  • Created 5 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
RamiAwarcommented, Apr 16, 2021

I’ve developed a wrapper around PyMongo and Pydantic that would make this easy, hope somebody finds this useful:

https://ramiawar.github.io/Mongomantic/

1reaction
manditricommented, Feb 3, 2022

Nice work @RamiAwar! Why is archived and read-only?

there are better alternatives out there now with full async support and fancy sprinkles

@RamiAwar what are these better alternatives that you mentioned. I have been looking for something in this direction. Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pydantic as mongo ORM · Issue #300 - GitHub
I've developed a MongoDB document mapper with pydantic. It converts pymongo find and find_one results to pydantic objects.
Read more >
ODMantic
Sync and Async ODM (Object Document Mapper) for MongoDB based on standard python type hints. Built on top of pydantic for model definition...
Read more >
Meet Beanie: A MongoDB ODM + Pydantic - Talk Python
You should be using an ODM. This time we're talking about Beanie which is one of the exciting, new MongoDB Object Document Mappers...
Read more >
mongomantic - PyPI
Mongomantic is a lightweight python MongoDB ORM based on Pydantic and PyMongo, heavily inspired by Mongoengine.
Read more >
How to use FastAPI with MongoDB - Python in Plain English
FastAPI uses the Pydantic library to check the data and process it. It is a fast and yet easy to use package. You...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found