The model is the core of everything RethinkORM deals with. All data returned from RethinkDB is eventually wrapped in the model before being returned to the end user. It provides an pythonic, object style interface for the data, exposing methods to save and update documents along with creating new ones.

Quick Start:

pip install RethinkORM

First we need to make an object which will represent all of our data in a specific table, along with getting a connection to RethinkDB started.

import rethinkdb as r
from rethinkORM import RethinkModel


class tvProps(RethinkModel):
    table = "stargate_props"

For more information on what class properties are available to change, see rethinkORM

Inserting/creating an entry

dhdProp = tvProps(what="DHD", planet="P3X-439", description="Dial HomeDevice")"DHD_P3X_439"

Updating an entry

updatedProp = tvProps("DHD_P3X_439")
updatedProp.description="""Dial Home Device from the planel P3X-439, where an
    Ancient Repository of Knowledge was found, and interfaced with by Colonel

Deleting an entry

oldProp = tvProps("DHD_P3X_439")

rethinkModel Module

class rethinkORM.rethinkModel.RethinkModel(id=False, **kwargs)[source]

Emulates a python object for the data which is returned from rethinkdb and the official Python client driver. Raw data from the database is stored in _data to keep the objects namespace clean. For more information look at how _get() and _set() function in order to keep the namespace cleaner but still provide easy access to data.

This object has a __repr__ method which can be used with print or logging statements. It will give the id and a representation of the internal _data dict for debugging purposes.

table = ''

The table which this document object will be stored in

primaryKey = 'id'

The current primary key of the table

durability = 'soft'

Can either be Hard or Soft, and is passed to RethinkDB

non_atomic = False

Determins if the transaction can be non atomic or not

upsert = True

Will either update, or create a new object if true and a primary key is given.

__init__(id=False, **kwargs)[source]

Initializes the main object, if id is in kwargs, then we assume this is already in the database, and will try to pull its data, if not, then we assume this is a new entry that will be inserted.

(Optional, only if not using .repl()) conn or connection can also be passed, which will be used in all the .run() clauses.


A hook called at the end of the main __init__ to allow for custom inherited classes to customize their init process without having to redo all of the existing int. This should accept nothing besides self and nothing should be returned.


Deletes the given item from the objects _data dict, or if from the objects namespace, if it does not exist in _data.


Allows for the use of syntax similar to:

if "blah" in model:

This only works with the internal _data, and does not include other properties in the objects namepsace.

classmethod new(**kwargs)[source]

Creates a new instance, filling out the models data with the keyword arguments passed, so long as those keywords are not in the protected items array.

classmethod create(id=None, **kwargs)[source]

Similar to new() however this calls save() on the object before returning it.

classmethod find(id)[source]

Loads an existing entry if one can be found, otherwise an exception is raised.

Parameters:id (Str) – The id of the given entry
Returns:cls instance of the given id entry

If an id exists in the database, we assume we’ll update it, and if not then we’ll insert it. This could be a problem with creating your own id’s on new objects, however luckily, we keep track of if this is a new object through a private _new variable, and use that to determine if we insert or update.


Deletes the current instance. This assumes that we know what we’re doing, and have a primary key in our data already. If this is a new instance, then we’ll let the user know with an Exception


Allows for the representation of the object, for debugging purposes


Provides a cleaner interface to dynamically add items to the models list of protected functions to not store in the database

Table Of Contents

Previous topic

RethinkORM: Introduction

Next topic


This Page