A Pythonic, object-oriented interface for working with MongoDB.
MongoDB has paused the development of PyMODM. If there are any users who want
to take over and maintain this project, or if you just have questions, please respond
to this forum post <https://developer.mongodb.com/community/forums/t/updates-on-pymodm/9363>
_.
.. image:: https://readthedocs.org/projects/pymodm/badge/?version=stable :alt: Documentation :target: http://pymodm.readthedocs.io/en/stable/?badge=stable
.. image:: https://travis-ci.org/mongodb/pymodm.svg?branch=master :alt: View build status :target: https://travis-ci.org/mongodb/pymodm
A generic ODM around PyMongo_, the MongoDB Python driver. PyMODM works on Python
2.7 as well as Python 3.3 and up. To learn more, you can browse the official documentation
_ or take a look at some examples
_.
.. _PyMongo: https://pypi.python.org/pypi/pymongo .. _official documentation: http://pymodm.readthedocs.io/en/stable .. _examples: https://github.com/mongodb/pymodm/tree/master/example
PyMODM is a "core" ODM, meaning that it provides simple, extensible functionality that can be leveraged by other libraries to target platforms like Django. At the same time, PyMODM is powerful enough to be used for developing applications on its own. Because MongoDB engineers are involved in developing and maintaining the project, PyMODM will also be quick to adopt new MongoDB features.
For issues with, questions about, or feedback for PyMODM, please look into
our support channels <http://www.mongodb.org/about/support>
. Please do not
email any of the PyMODM developers directly with issues or questions -
you're more likely to get an answer on the MongoDB Community Forums <https://developer.mongodb.com/community/forums/tags/c/drivers-odms-connectors/7/pymodm-odm>
.
Think you’ve found a bug? Want to see a new feature in PyMODM? Please open a case in our issue management tool, JIRA:
Create an account and login <https://jira.mongodb.org>
_.the PYMODM project <https://jira.mongodb.org/browse/PYMODM>
_.Bug reports in JIRA for all driver projects (e.g. PYMODM, PYTHON, JAVA) and the Core Server (i.e. SERVER) project are public.
Please include all of the following information when opening an issue:
Detailed steps to reproduce the problem, including full traceback, if possible.
The exact python version used, with patch level::
$ python -c "import sys; print(sys.version)"
The exact version of PyMODM used, with patch level::
$ python -c "import pymodm; print(pymodm.version)"
The PyMongo version used, with patch level::
$ python -c "import pymongo; print(pymongo.version)"
The operating system and version (e.g. Windows 7, OSX 10.8, ...)
Web framework or asynchronous network library used, if any, with version (e.g. Django 1.7, mod_wsgi 4.3.0, gevent 1.0.1, Tornado 4.0.2, ...)
If you’ve identified a security vulnerability in a driver or any other
MongoDB project, please report it according to the instructions here <http://docs.mongodb.org/manual/tutorial/create-a-vulnerability-report>
_.
Here's a basic example of how to define some models and connect them to MongoDB:
.. code-block:: python
from pymongo import TEXT from pymongo.operations import IndexModel from pymodm import connect, fields, MongoModel, EmbeddedMongoModel
connect('mongodb://localhost:27017/myApp')
class User(MongoModel): # Use 'email' as the '_id' field in MongoDB. email = fields.EmailField(primary_key=True) fname = fields.CharField() lname = fields.CharField()
class BlogPost(MongoModel): # This field references the User model above. # It's stored as a bson.objectid.ObjectId in MongoDB. author = fields.ReferenceField(User) title = fields.CharField(max_length=100) content = fields.CharField() tags = fields.ListField(fields.CharField(max_length=20)) # These Comment objects will be stored inside each Post document in the # database. comments = fields.EmbeddedModelListField('Comment')
class Meta:
# Text index on content can be used for text search.
indexes = [IndexModel([('content', TEXT)])]
class Comment(EmbeddedMongoModel): author = fields.ReferenceField(User) body = fields.CharField() vote_score = fields.IntegerField(min_value=0)
han_solo = User('[email protected]', 'Han', 'Solo').save() chewbacca = User( '[email protected]', 'Chewbacca', 'Thomas').save()
post = BlogPost( # Since this is a ReferenceField, we had to save han_solo first. author=han_solo, title="Five Crazy Health Foods Jabba Eats.", content="...", tags=['alien health', 'slideshow', 'jabba', 'huts'], comments=[ Comment(author=chewbacca, body='Rrrrrrrrrrrrrrrr!', vote_score=42) ] ).save()
slideshows = BlogPost.objects.raw({'tags': 'slideshow'})
slideshow_titles = slideshows.only('title')
print(slideshow_titles.first().title)