Skip to content

Commit bc2ae4e

Browse files
committed
docs
1 parent 2293c56 commit bc2ae4e

File tree

3 files changed

+47
-51
lines changed

3 files changed

+47
-51
lines changed

docs/source/examples/tutorial.rst

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.. _tutorial:
22

3+
.. module:: stdnet.orm
34

45
============================
56
Using Models
@@ -15,7 +16,7 @@ Writing Models
1516
==========================
1617

1718
Defining stdnet *models* is achieved by subclassing the
18-
:class:`stdnet.orm.StdModel` class. The following
19+
:class:`StdModel` class. The following
1920
snipped implements two models, ``Author`` and ``Book``::
2021

2122
from stdnet import orm
@@ -44,11 +45,11 @@ An application
4445

4546
Let's start with tutorial application: a small hedge fund.
4647
You never know it may become useful in the future!
47-
4848
The application uses the following three models,
4949

50-
* A ``Fund`` which stores information about several ``Position``
51-
* A ``Position`` is an investment on a particular funancial ``Instrument``.
50+
* ``Fund`` for storing information about several ``Position``.
51+
* ``Instrument`` holds information about tradable financial intruments.
52+
* ``Position`` is an investment on a particular ``Instrument`` in a ``Fund``.
5253

5354
A minimal ``stdnet`` implementation can look like this::
5455

@@ -87,45 +88,50 @@ A minimal ``stdnet`` implementation can look like this::
8788
8889
If you are familiar with django_ you will see several similarities and you should be able to understand,
8990
with a certain degree of confidence, what it is going on.
90-
The only difference is the ``prices`` :class:`stdnet.orm.ListField`
91+
The only difference is the ``prices`` :class:`ListField`
9192
in the ``Instrument`` model which is
9293
not available in a traditional relational database.
9394

9495
The metaclass
9596
~~~~~~~~~~~~~~~~~~~~~~~
96-
The ``Position`` models specifies a ``Meta`` class with an ``ordering`` attribute.
97+
The ``Position`` models specifies a ``Meta`` class with an ``ordering``
98+
attribute.
9799
When provided, as in this case, the Meta class fields are used by the ``orm``
98-
to customize the build of the :class:`stdnet.orm.Metaclass` for the model.
99-
In this case we instruct the mapper to manage the ``Position`` model
100-
as ordered with respect to the :class:`stdnet.orm.DateField` ``dt``
100+
to customise the build of the :class:`Metaclass` for the model. The metaclas
101+
is stored in the :attr:`StdModel._meta` attribute.
102+
103+
In this case we instruct the orm to manage the ``Position`` model
104+
as ordered with respect to the :class:`DateField` ``dt``
101105
in descending order. Check the :ref:`sorting <sorting>`
102106
documentation for more details or ordering and sorting.
103107

104108

105109
Registering Models
106110
================================
107111

108-
Before playing with the API you need to :ref:`register the models <register-model>`::
112+
Before playing with the API let's to :ref:`register the models <register-model>`
113+
to a backend server. Registration is not compulsory, but it is required when using
114+
model's :class:`Manager`::
109115

110116
import orm
111117

112118
orm.register(Fund, 'redis://my.host.name:6379/?db=1')
113119
orm.register(Instrument, 'redis://my.host.name:6379/?db=1')
114-
orm.register(Position, 'redis://my.host.name:6379/?db=2')
120+
orm.register(Position, 'redis://my.host.name:6379/?db=1')
115121
116122

117123
.. _one-to-many:
118124

119125
One-to-many relationships
120126
================================
121127

122-
The *Position* model contains two :class:`stdnet.orm.ForeignKey` fields.
128+
The *Position* model contains two :class:`ForeignKey` fields.
123129
In the context of relational databases a
124130
`foreign key <http://en.wikipedia.org/wiki/Foreign_key>`_ is
125131
a referential constraint between two tables.
126132

127133
For stdnet is exactly the same thing. The field store the ``id`` of a
128-
related :class:`stdnet.orm.StdModel` instance.
134+
related :class:`StdModel` instance.
129135
Behind the scenes, this functionality is implemented by Python descriptors_.
130136
This shouldn't really matter to you, but we point it out here for the curious.
131137

@@ -142,7 +148,7 @@ update and delete objects.
142148
Creating objects
143149
~~~~~~~~~~~~~~~~~~~~~
144150

145-
An instance of a :class:`stdnet.orm.StdModel`, an object for clarity,
151+
An instance of a :class:`StdModel`, an object for clarity,
146152
is mapped to a hash table in the :class:`stdnet.BackendDataServer` backend.
147153
To create an object, instantiate it using keyword arguments to the
148154
model class, then call ``save()`` to save it to the data-server.

docs/source/model/models.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,13 @@ TS
173173
Registration
174174
======================
175175

176-
Once a model is defined, in order to use it in an application it needs to be
177-
registered with a back-end database. Unregistered models cannot be used to save
178-
data and they will raise exceptions.
176+
Models can be registered with a :class:`stdnet.BackendDataServer` so that
177+
the model :class:`Manager` can be used to create instance and query the database.
178+
If a model is not registered, the only way to operate on it is via the
179+
:class:`Session` API.
179180

180-
Stdnet comes with two functions for
181-
registration, a low level and a higher level one which can be used to register
182-
several models at once.
181+
Stdnet provides two registration functions, a low level and a higher level one
182+
which can be used to register several models at once.
183183

184184
Register
185185
~~~~~~~~~~~~~~~~

stdnet/orm/mapper.py

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,20 @@ def flush_models(includes = None, excludes = None):
6060

6161
def register(model, backend = None, ignore_duplicates = True,
6262
local_thread = False, **params):
63-
'''The low level function for registering a :class:`stdnet.orm.StdModel`
64-
classes with a :class:`stdnet.backends.BackendDataServer` data server.
63+
'''The low level function for registering a :class:`StdModel`
64+
classes with a :class:`stdnet.BackendDataServer` data server.
6565
66-
:parameter model: a :class:`stdnet.orm.StdModel` class. Must be provided.
66+
:parameter model: a :class:`StdModel`. Must be provided.
6767
6868
:parameter backend: a backend connection string.
6969
For example::
7070
7171
redis://localhost:8080?db=6&prefix=bla.
7272
7373
Default ``settings.DEFAULT_BACKEND``.
74-
74+
7575
:parameter params: optional parameters which can be used to override the
7676
connection string parameters.
77-
78-
:parameter timeout: timeout in seconds for keys persistence.
79-
If not provided it is calculated from the
80-
connection string.
81-
82-
Default ``None``.
8377
8478
**Usage**
8579
@@ -90,7 +84,7 @@ def register(model, backend = None, ignore_duplicates = True,
9084
orm.register(Author, 'redis://my.host.name:6379/?db=1')
9185
orm.register(Book, 'redis://my.host.name:6379/?db=2')
9286
orm.register(MyOtherModel,
93-
'redis://my.host.name:6379/?db=2&keyprefix=differentprefix')
87+
'redis://my.host.name:6379/?db=2&keyprefix=differentprefix.')
9488
9589
``my.host.name`` can be ``localhost`` or an ip address or a domain name,
9690
while ``db`` indicates the database number (very useful for separating data
@@ -130,17 +124,17 @@ def unregister(model = None):
130124

131125

132126
def registered_models():
133-
'''Iterator over registered models'''
127+
'''An iterator over registered models'''
134128
return (m for m in _GLOBAL_REGISTRY)
135129

136130

137131
def model_iterator(application):
138-
'''\
139-
Returns a generatotr of :class:`stdnet.orm.StdModel` classes found
140-
in the ``models`` module of an ``application`` dotted path.
132+
'''A generator of :class:`StdModel` classes found in *application*.
141133
142-
:parameter application: An iterable over python dotted-paths
143-
where models are defined.
134+
:parameter application: A python dotted path or an iterable over python
135+
dotted-paths where models are defined.
136+
137+
Only models defined in these paths are considered.
144138
145139
For example::
146140
@@ -164,13 +158,12 @@ def model_iterator(application):
164158
try:
165159
mod_models = import_module('.models',application)
166160
except ImportError:
167-
raise StopIteration
168-
169-
label = getattr(mod_models,'app_label',label)
161+
mod_models = mod
162+
label = getattr(mod_models, 'app_label', label)
170163
for name in dir(mod_models):
171-
model = getattr(mod_models,name)
172-
meta = getattr(model,'_meta',None)
173-
if isinstance(model,StdNetType) and meta:
164+
model = getattr(mod_models, name)
165+
meta = getattr(model, '_meta', None)
166+
if isinstance(model, StdNetType) and meta:
174167
if not meta.abstract and meta.app_label == label:
175168
yield model
176169

@@ -182,21 +175,18 @@ def register_application_models(applications,
182175
'''\
183176
A higher level registration functions for group of models located
184177
on application modules.
185-
186-
It uses the :func:`stdnet.orm.model_iterator` function to iterate
187-
through all :class:`stdnet.orm.StdModel` models available in ``applications``
188-
and register them using the :func:`stdnet.orm.register` low
189-
level function.
178+
It uses the :func:`model_iterator` function to iterate
179+
through all :class:`StdModel` models available in ``applications``
180+
and register them using the :func:`register` low level function.
190181
191182
:parameter applications: A String or a list of strings which represent
192-
python dotted paths to modules containing
193-
a ``models`` module where models are implemented.
183+
python dotted paths where models are implemented.
194184
:parameter models: Optional list of models to include. If not provided
195185
all models found in *applications* will be included.
196186
:parameter app_defaults: optional dictionary which specify a model and/or
197187
application backend connection string.
198188
:parameter default: The default connection string.
199-
:rtype: a generator over registered models
189+
:rtype: A generator over registered :class:`StdModel`.
200190
201191
For example::
202192

0 commit comments

Comments
 (0)