LEFT | RIGHT |
1 .. _topics-models: | 1 .. _topics-models: |
2 | 2 |
3 ====== | 3 ====== |
4 Models | 4 Models |
5 ====== | 5 ====== |
6 | 6 |
7 A model represents a single business logic or concept. It contains fields and | 7 A model represents a single business logic or concept. It contains fields and |
8 defines the behaviors of the data. Most of the time, each model stores data in | 8 defines the behaviors of the data. Most of the time, each model stores data in |
9 a single database table. | 9 a single database table. |
10 | 10 |
11 The basics: | 11 The basics: |
12 | 12 |
13 * Each model is a Python class that subclasses one of | 13 * Each model is a Python class that subclasses one of |
14 :class:`trytond.model.model.Model`. | 14 :class:`trytond.model.model.Model`. |
15 | 15 |
16 * :ref:`Fields <topics-fields>` are defined as model attributes. | 16 * :ref:`Fields <ref-models-fields>` are defined as model attributes. |
17 | 17 |
18 * Tryton generates the tables definition and provides an API to access the | 18 * Tryton generates the table definitions and provides an API to access the |
19 data. | 19 data. |
20 | 20 |
21 Quick example | 21 Example |
22 ============= | 22 ======= |
23 | 23 |
24 This example model defines a ``Party`` which has a ``name`` and a ``code``:: | 24 This example defines a ``Party`` model which has a ``name`` and a ``code`` |
| 25 fields:: |
25 | 26 |
26 from trytond.model import ModelView, ModelSQL, fields | 27 from trytond.model import ModelView, ModelSQL, fields |
27 | 28 |
28 class Party(ModelSQL, ModelView): | 29 class Party(ModelSQL, ModelView): |
29 "Party" | 30 "Party" |
30 _description = __doc__ | 31 _description = __doc__ |
31 _name = "party.party" | 32 _name = "party.party" |
32 name = fields.Char('Name') | 33 name = fields.Char('Name') |
33 code = fields.Char('Code') | 34 code = fields.Char('Code') |
34 | 35 |
35 Party() | 36 Party() |
36 | 37 |
37 Instantiating the class registers the model class in the framework. Later the | 38 Instantiating the class registers the model class in the framework. Later the |
38 class will be instantiated once per database and stored in the | 39 class will be instantiated once per database and stored in the |
39 :ref:`Pool <topics-pool>`. Model instances are essentially accessors to | 40 :ref:`Pool <topics-pool>`. Model instances are essentially accessors to |
40 records. | 41 records. |
41 | 42 |
42 Model properties define meta-information of the model, they are class | 43 Model attributes define meta-information of the model. They are class |
43 attributes starting with an underscore. Some Model Properties are instance | 44 attributes starting with an underscore. Some model properties are instance |
44 attributes allowing to update them at other places in the framework. | 45 attributes allowing to update them at other places in the framework. |
LEFT | RIGHT |