Models

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.

General Usage

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

r.connect(db="props").repl()


class tvProps(RethinkModel):
    table = "stargate_props"

Inserting/creating an entry

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

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
    Jack."""
updatedProp.save()

Deleting an entry

oldProp = tvProps("DHD_P3X_439")
oldProp.delete()

RethinkORMException

class rethinkORM.RethinkORMException

Gotta catch them all!

RethinkModel

class rethinkORM.RethinkModel(id=None, **kwargs)

This mostly emulates a basic python object for the returned data from RethinkDB. You can use both attribute access and item access to interact with the data.

RethinkModel.table = ''

The table which this document object will be stored in

RethinkModel.primary_key = 'id'

The current primary key in use on the table. This defaults to id as RethinkDB will default to using id as the primary key.

Warning

In pre v1.0.0 releases, this was called primaryKey

RethinkModel.durability = 'soft'

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

RethinkModel.__init__(id=None, **kwargs)

Initializes the main object, if id is the only thing passed then we assume this document is already in the database and grab its data, otherwise we treat it as a new object.

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

RethinkModel.finish_init()

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.

RethinkModel.__delitem__(item)

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

RethinkModel.__contains__(item)

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 RethinkModel.new(id=None, **kwargs)

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 RethinkModel.create(id=None, **kwargs)

Similar to new() however this calls save() on the object before returning it, to ensure that it is already in the database. Good for make and forget style calls.

RethinkModel.save()

Update or insert the document into the database.

RethinkModel.delete()

Deletes the current instance, if its in the database (or try).

RethinkModel.__repr__()

Allows for the representation of the object, for debugging purposes

RethinkModel.protected_items

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

Warning

In the pre v1.0.0 releases, this was called protectedItems