Django and RethinkDB: A tutorial

Django and RethinkDB: A tutorial

I’ve been using Django for a while and I’m a huge supporter of it. Late last year I picked up RethinkDB and instantly fell in-love with it. It’s quickly become my go-to NoSQL database for all my new up-and-coming projects. In this article I will give you a crash course in using RethinkDB with Django — the Django way.

Note: This is purely an overview example. Inference and extrapolation is required.

Dependencies

  1. RethinkDB v1.4.1+
  2. Django 1.4+
  3. Python 2.7
  4. RethinkDB python driver (pip install rethinkdb)
  5. Rush mix-tape

Connections

Connecting to RethinkDB in Django isn’t too hard. You just need a custom middleware component. Also, personally I use repl(), but you don’t have to.

Middleware code

Connecting to middleware

You then need to plug that code in to Django’s middleware definition.

 

CRUD you! A sample views.py

 

Creating

 Reading

 Deleting

 Updating

Complete source

 

Threading

An important thing to note when threading is that your RethinkDB connection may not follow your thread. I haven’t done threading testing in v1.4.1 yet, but in the past I had to establish a new connection for my threads to work. So keep that in-mind.

 

Dave

About David Parlevliet

Dave is long time developer with a passion toward teaching. He divides his time between his wife, her cat and his projects. He recently started using twitter so make sure to follow him!