RethinkModel

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

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


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")
dhdProp.id="DHD_P3X_439"
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()

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.

finishInit()[source]

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.

__delitem__(item)[source]

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

__contains__(item)[source]

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
save()[source]

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.

delete()[source]

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

__repr__()[source]

Allows for the representation of the object, for debugging purposes

protectedItems[source]

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

RethinkCollection

This Page