Documentation for python-stdnet's DEVELOPMENT version. Get the release docs here.

Registration

Registration consists in associating a StdModel to a Manager via a Router. In this way one can have a group of models associated with their managers pointing at their, possibly different, back-end servers. Registration is straightforward and as shown in the tutorial application it is achieved by:

from stdnet import odm

models = odm.Router('redis://127.0.0.1:6379?db=7&password=bla')

models.register(Instrument)
models.register(Fund)
models.register(Position)

The connection string passed as first argument when initialising a Router, is the default back-end of that Router. It is possible to register models to a different back-end by passing a connection string to the Router.register() method:

models.register(MyModel, 'redis://127.0.0.1:6379?db=8&password=bla')

Accessing managers

Given a models Router there are two ways one can access a model Manager to perform database queries.

Dictionary interface

The most straightforward and intuitive way, for accessing managers, is to use the Router as a dictionary of Manager:

# Create a Query for Instrument
query = models[Instrument].query()
#
# Create a new Instrument and save it to the backend server
inst = models[Instrument].new(...)

Dotted notation

An alternative to the dictionary interface is the dotted notation, where a manager can be accessed as an attribute of the Router, the attribute name is given by the StdModel metaclass name. This is the ModelMeta.name attribute of the Model._meta object. It is, by default, the class name of the model in lower case:

query = models.instrument.query()
#
inst = models.instrument.new(...)

This interface is less verbose than the dictionary notation and, importantly, it reduces to zero the imports one has to write on python modules using your application, in other words it makes your application less dependent on the actual implementation of StdModel.

Multiple backends

The Router allows to use your models in several different back-ends without changing the way you query your data. In addition it allows to specify different back-ends for write operations and for read only operations.

To specify a different back-end for read operations one registers a model in the following way:

models.register(Position, 'redis://127.0.0.1:6379?db=8&password=bla',
                'redis://127.0.0.1:6380?db=1')

Custom Managers

When a Router registers a StdModel, it creates a new instance of a Manager and add it to the dictionary of managers. It is possible to supply a custom manager class by specifying the manager_class attribute on the StdModel:

class CustomManager(odm.Manager):

    def special_query(self, ...):
        return self.query().filter(...)


class MyModel(odm.StdModel):
    ...

    manager_class = CustomManager

Model Iterator

stdnet.odm.model_iterator(application, include_related=True, exclude=None)

A generator of StdModel classes found in application.

Parameters:application – A python dotted path or an iterable over python dotted-paths where models are defined.

Only models defined in these paths are considered.

For example:

from stdnet.odm import model_iterator

APPS = ('stdnet.contrib.searchengine',
        'stdnet.contrib.timeseries')

for model in model_iterator(APPS):
    ...

Table Of Contents

Previous topic

Using Models

Next topic

Query your Data

This Page