Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

MongoDB hearts Django? (Django NYC)

12,706 views

Published on

A look at how to work with MongoDB and Django, and how we can make the interaction even better in the future.

Published in: Technology

MongoDB hearts Django? (Django NYC)

  1. 1. open-source, high-performance, schema-free, document-oriented database http://www.mongodb.org/
  2. 2. The Web framework for perfectionists with deadlines http://www.djangoproject.com/
  3. 3. “One size fits all” no longer RDBMS (Oracle, MySQL) Non-relational New gen. OLAP (vertica, aster, greenplum) operational stores (“NoSQL”)
  4. 4. NoSQL really means: non-relational next generation operational datastores and databases
  5. 5. Scaling out no joins + light transactional semantics = horizontally scalable architectures
  6. 6. Data models no joins + light transactional semantics = horizontally scalable architectures important side effect : new data models = improved ways to develop applications
  7. 7. Data models Key/value memcached, dynamo, voldemort Tabular bigtable, cassandra, hbase, hypertable Document-oriented couchdb,
  8. 8. MongoDB in two minutes • Documents (think rows) are dicts: {“hello”: “world”} {“hello”: “world”, “foo”: [{“bar”: 1}]} • Collections (think tables) are schema-free • Queries are dynamic • Great single node performance • Built in replication and auto-sharding • No complex transactions
  9. 9. ?
  10. 10. Yes... ...but also sometimes no
  11. 11. Similar to + • A lot of Django doesn’t depend on django.db: • URL dispatch, templates, I18N, caching, etc. • Some things do: • Models • Auth • Sessions • Admin
  12. 12. settings.py DATABASE_ENGINE = '' DATABASE_NAME = '' DATABASE_USER = '' DATABASE_PASSWORD = '' DATABASE_HOST = '' DATABASE_PORT = '' MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', # 'django.contrib.sessions.middleware.SessionMiddleware', # 'django.contrib.auth.middleware.AuthenticationMiddleware', ) INSTALLED_APPS = ( # 'django.contrib.auth', 'django.contrib.contenttypes', # 'django.contrib.sessions', 'django.contrib.sites', )
  13. 13. Representing a Poll {'question': 'Do MongoDB + Django <3 each other?', 'pub_date': datetime.datetime(2010, 1, 21), 'choices': [{'votes': 35, 'choice': 'Yes!'}, {'votes': 2, 'choice': 'No...'}]}
  14. 14. models.py (PyMongo) def save_poll(question): return db.polls.insert({"question": question, "pub_date": datetime.utcnow()}) def all_polls(): return db.polls.find() def add_choice(poll_id, choice): db.polls.update({"_id": poll_id}, {"$push": {"choices": {"choice": choice, "votes": 0}}}) def add_vote(poll_id, choice): db.polls.update({"_id": poll_id}, {"$inc": {"choices.%d.votes" % choice: 1}}) http://api.mongodb.org/python
  15. 15. models.py (MongoKit) class Poll(mongokit.Document): structure = {"question": str, "pub_date": datetime, "choices": [{"choice": str, "votes": int}]} required_fields = ["question"] default_values = {"pub_date": datetime.utcnow()} http://bytebucket.org/namlook/mongokit
  16. 16. models.py (Ming) class Poll(ming.Document): class __mongometa__: session = session name = "polls" _id = ming.Field(ming.schema.ObjectId) question = ming.Field(str, required=True) pub_date = ming.Field(datetime.datetime, if_missing=datetime.datetime.utcnow) choices = ming.Field([{"choice": str, "votes": int}]) http://merciless.sourceforge.net/
  17. 17. mango - sessions and auth • Full sessions support • mango provided User class • supports is_authenticated(), set_password(), etc. http://github.com/vpulim/mango
  18. 18. mango - sessions and auth SESSION_ENGINE = 'mango.session' AUTHENTICATION_BACKENDS = ('mango.auth.Backend',) MONGODB_HOST = 'localhost' MONGODB_PORT = None MONGODB_NAME = 'mydb' http://github.com/vpulim/mango
  19. 19. What about admin? • No great solution... yet. • Could replace admin app like mango does for sessions / auth • Or...
  20. 20. Supporting MongoDB in django.db • Best solution (long term) • http://bitbucket.org/kpot/django-mongodb/ • http://bitbucket.org/wkornewald/django-nonrel/ • http://code.djangoproject.com/wiki/NonSqlBackends
  21. 21. 1. Download MongoDB http://www.mongodb.org 2. Try it out! 3. (And help us make things work better with Django!)
  22. 22. • http://www.mongodb.org • irc.freenode.net#mongodb • mongodb-user on google groups • @mongodb, @mdirolf • mike@10gen.com • http://www.slideshare.net/mdirolf • http://github.com/mdirolf/djanMon/

×