MongoDB hearts Django? (Django NYC)

mdirolf 3,686 views 24 slides Jan 21, 2010
Slide 1
Slide 1 of 24
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24

About This Presentation

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


Slide Content

open-source, high-performance,
schema-free, document-oriented
database
http://www.mongodb.org/

The Web framework for perfectionists
with deadlines
http://www.djangoproject.com/

“One size fits all”
no longer
RDBMS
(Oracle, MySQL)
New gen. OLAP
(vertica, aster, greenplum)
Non-relational
operational stores
(“NoSQL”)

NoSQL really means:
non-relational next generation
operational datastores and databases

Scaling out
no joins +
light transactional semantics =
horizontally scalable architectures

Data models
no joins +
light transactional semantics =
horizontally scalable architectures
important side e!ect :
new data models =
improved ways to develop
applications

Data models
Key/value
memcached, dynamo, voldemort
Tabular
bigtable, cassandra, hbase, hypertable
Document-oriented
couchdb,

MongoDB in two minutes
•Documents (think rows) are dicts:
•Collections (think tables) are schema-free
•Queries are dynamic
•Great single node performance
•Built in replication and auto-sharding
•No complex transactions
{“hello”: “world”}
{“hello”: “world”, “foo”: [{“bar”: 1}]}

?

Yes...
...but also sometimes no

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

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',
)

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...'}]}

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

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

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/

mango - sessions and auth
http://github.com/vpulim/mango
•Full sessions support
•mango provided User class
•supports is_authenticated(), set_password(), etc.

mango - sessions and auth
http://github.com/vpulim/mango
SESSION_ENGINE = 'mango.session'
AUTHENTICATION_BACKENDS = ( 'mango.auth.Backend' ,)
MONGODB_HOST = 'localhost'
MONGODB_PORT = None
MONGODB_NAME = 'mydb'

What about admin?
•No great solution... yet.
•Could replace admin app like mango does
for sessions / auth
•Or...

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

1.Download MongoDB
http://www.mongodb.org
2.Try it out!
3.(And help us make things work better with
Django!)

•http://www.mongodb.org
•irc.freenode.net#mongodb
•mongodb-user on google groups
•@mongodb, @mdirolf
[email protected]
•http://www.slideshare.net/mdirolf
•http://github.com/mdirolf/djanMon/