rethinkmodel.model - Model definition and methods¶
Module to create and manage your models.
Each data object must inherit from Model
class and contains
some annotations to define the types. Types can be “simples” or “Model” children.
Each Model has got:
id: that is set from database (None by default)
created_on: the creation date of the data (never changed)
updated_on: the modification date, change each time you save the model
deleted_on: if you set
rethinkdb.db.SOFT_DELETE
toTrue
in configuration, so the model is never deleted but this date is set. Rethink:Model will filter objects to not get soft deleted objects
class User(Model):
username: str # a string
tags: List[str] # a List if string, connot be None
categories: Optional[List[str]] # list of string, can be None
@classmethod
def get_index(cls):
''' Create an index on "name" property '''
return ['name']
class Project(Model):
# bind an User id here, because User is a model
owner: User
name: str # other field
comment: Optional[str] # Optional => "None" is accepted
To get Linked objects, it’s possible to use “join()” method
# The user will be fetched and
# projects property is set with all
# related projects.
# This is because "Project" object
# has got a User reference field.
user = User.get(id).join(Project)
See Model methods documentation to have a look on arguments (like limit, offset, …)
-
class
rethinkmodel.model.
BaseModel
[source]¶ Base Model interface.
To change the tablename and avoid name generation, you may use
__tablename__
static property.-
classmethod
get_indexes
() → Optional[Union[List, Dict]][source]¶ You can override this method to return a list of indexes.
This method is called by
rethinkmodel.manage.auto()
function to prepare indexes in database.Warning
This is a work in progress. At this time, you can only return a list of strings (name of properties to use as index)
-
classmethod
-
class
rethinkmodel.model.
Model
(**kwargs)[source]¶ Model is the parent class of all tables for RethinkDB.
The constructor accepts kwargs with attributes to set. For example, if the
User
class is set like this:class User(Model): username: str
You may construct the object with:
user = User(username="John")
-
classmethod
changes
(select: Optional[Union[Dict, Callable]] = None) → Generator[source]¶ Get a feed Generator which reacts on changes.
This return a blocking cursor tuple where the first element is the “old value” and the second is the “new value”. It is very usefull to make action when something changed in database.
feed = User.changes() for oldval, newval in feed: # here, we have changes from oldval to newval # when a row changed in "users" table.
The “select” argument is the same as in
rethinkmodel.model.Model.filter()
, it can be adict
or a callable function or lambda to filter data.feed = User.changes(select=lambda x = x['name'].eq("Foo")) for oldval, newval in feed: # only on user named "Foo"
-
classmethod
filter
(select: Optional[Union[Dict, Callable]] = None, limit: Optional[int] = None, offset: Optional[int] = None, order_by: Optional[Union[Dict, str]] = None) → List[rethinkmodel.model.Model][source]¶ Select object in database with filters.
The
select
argument can take adict
or aCallable
where RethinkDB row is accessible.# simple filter, all key: value is treated like a "and" filtered = User.filter(select={ "name": "foo" }) # Get user with name = "user1" or "user2". # Here, the lambda function will get res as a row. So the # RethinkDB methods are accessible like "or_", "and_", "between", ... filtered = User.filter( select=lambda res: res["name"].eq("user0").or_(res["name"].eq("user2")), )
Warning
Inside lambda (or any Callable), the argument is a RethinkDB row object. You will need to use specific RethinkDB methods to make the filter to work.
-
classmethod
get
(data_id: Optional[str]) → Optional[rethinkmodel.model.Model][source]¶ Return a Model object fetched from database for the giver ID.
-
classmethod
get_all
(limit: Optional[int] = None, offset: Optional[int] = None, order_by: Optional[Union[Dict, str]] = None) → List[rethinkmodel.model.Model][source]¶ Get collection of results.
-
join
(*models: Type[rethinkmodel.model.Model], limit: Optional[int] = None, offset: Optional[int] = None, order_by: Optional[Union[Dict, str]] = None) → rethinkmodel.model.Model[source]¶ Join linked models to the current model, fetched by id.
-
save
() → rethinkmodel.model.Model[source]¶ Insert or update data if self.id is set.
Return the save object (self)
-
classmethod