8000 chapter 5 - connect the dots · odoo/documentation@dbe3410 · GitHub
[go: up one dir, main page]

Skip to content

Commit dbe3410

Browse files
committed
chapter 5 - connect the dots
1 parent 22c61a8 commit dbe3410

15 files changed

+1170
-168
lines changed

content/contributing/development/coding_guidelines.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,8 @@ In general in Odoo, when manipulating strings, prefer ``%`` over ``.format()``
813813
of position (when multiple variables have to be replaced). This makes the
814814
translation easier for the community translators.
815815

816+
.. _contributing/coding_guidelines/model_members:
817+
816818
Symbols and Conventions
817819
-----------------------
818820

content/developer/reference/backend/orm.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,8 @@ behavior is desired:
518518
:class:`~odoo.fields.Many2one`
519519
:type: :class:`~odoo.addons.base.models.res_company`
520520

521+
.. _reference/orm/recordsets:
522+
521523
Recordsets
522524
==========
523525

content/developer/tutorials/server_framework_101.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Ready? Let's get started!
3939
- :doc:`server_framework_101/02_lay_the_foundations`
4040
- :doc:`server_framework_101/03_build_user_interface`
4141
- :doc:`server_framework_101/04_relational_fields`
42-
- :doc:`server_framework_101/05_business_logic`
42+
- :doc:`server_framework_101/05_connect_the_dots`
4343
- :doc:`server_framework_101/06_security`
4444
- :doc:`server_framework_101/07_advanced_views`
4545
- :doc:`server_framework_101/08_inheritance`

content/developer/tutorials/server_framework_101/02_lay_the_foundations.rst

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,15 @@ classes, fields are defined as class attributes. Each field is an instance of a
6565
`odoo.fields` package. For example, `Char`, `Float`, `Boolean`, each designed to handle different
6666
types of data. When defining a field, developers can pass various arguments to finely control how
6767
data is handled and presented in Odoo. For example, `string` defines the label for the field in the
68-
user interface, `help` provides a tooltip when hovering the field in the user interface, and
69-
`required` makes filling in the field mandatory.
68+
user interface, `help` provides a tooltip when hovering the field in the user interface, `required`
69+
makes filling in the field mandatory, and `default` provides a default field value.
7070

7171
Individual data entries are called **records**. They are based on the structure defined by models
7272
and contain the actual data for each field specified in the model. In Python, records are
7373
represented as instances of the model's class, allowing developers to interact with data using
7474
object-oriented programming techniques. For example, in a real estate application using a tenant
7575
model, each specific tenant (such as "Bafien Carpink") would be a separate record of that model.
7676

77-
.. seealso::
78-
For the full list of fields and their attributes, see the :ref:`reference documentation
79-
<reference/orm/fields>`.
80-
8177
.. example::
8278
Before we dive into creating our own models, let's take a look at a basic example of a model that
8379
represents storable products. It defines a `product` model with the `Product` class inheriting
@@ -94,12 +90,12 @@ model, each specific tenant (such as "Bafien Carpink") would be a separate recor
9490
9591
name = fields.Char(string="Name", required=True)
9692
description = fields.Text(string="Description")
97-
price = fields.Float(string="Sale Price", required=True)
93+
price = fields.Float(string="Sales Price", required=True)
9894
category = fields.Selection(
9995
string="Category",
10096
help="The category of the product; if none are suitable, select 'Other'.",
10197
selection=[
102-
('apparel', "Clothing")
98+
('apparel', "Clothing"),
10399
('electronics', "Electronics"),
104100
('home_decor', "Home Decor"),
105101
('other', "Other"),
@@ -116,11 +112,14 @@ model, each specific tenant (such as "Bafien Carpink") would be a separate recor
116112
- `category` is a `Selection` field with predefined options, each defined by a technical code
117113
and a corresponding label. Since it is required, a default value is provided.
118114

115+
.. seealso::
116+
For the full list of fields and their attributes, see the :ref:`reference documentation
117+
<reference/orm/fields>`.
118+
119119
Building on these new concepts, let's now create the first model for our real estate app. We'll
120120
create a model with some fields to represent real estate properties and their characteristics.
121121

122122
.. exercise::
123-
124123
#. Create a new :file:`real_estate_property.py` file at the root of the `real_estate` module.
125124
#. Update the :file:`real_estate/__init__.py` file to relatively import the
126125
:file:`real_estate_property.py` file, like so:
@@ -132,19 +131,20 @@ create a model with some fields to represent real estate properties and their ch
132131
#. Define a new model with `real.estate.property` as `_name` and a short `_description`.
133132
#. Add fields to represent the following characteristics:
134133

135-
- Name (required)
136-
- Description
137-
- Image (max 600x400 pixels)
138-
- Active (default to true)
139-
- State (new, offer received, under option, or sold; required; default to new)
140-
- Type (house, apartment, office building, retail space, or warehouse; required; default to
134+
- **Name** (required)
135+
- **Description**
136+
- **Image** (max 600x400 pixels)
137+
- **Active** (whether the property listing is active; defaults to true)
138+
- **State** (new, offer received, under option, or sold; required; defaults to new)
139+
- **Type** (house, apartment, office building, retail space, or warehouse; required; defaults to
141140
house)
142-
- Selling Price (without currency; with help text; required)
143-
- Availability Date (default to creation date + two months)
144-
- Floor Area (in square meters; with help text)
145-
- Number of Bedrooms (default to two)
146-
- Whether there is a garden
147-
- Whether there is a garage
141+
- **Selling Price** (without currency; with help text; required)
142+
- **Availability Date**
143+
- **Floor Area** (in square meters; with help text)
144+
- **Number of Bedrooms** (defaults to two)
145+
- **Garage** (whether there is a garage)
146+
- **Garden** (whether there is a garden)
147+
- **Garden Area** (in square meters; with help text)
148148

149149
.. tip::
150150
- The class name doesn't matter, but the convention is to use the model's upper-cased `_name`
@@ -163,7 +163,6 @@ create a model with some fields to represent real estate properties and their ch
163163
:caption: `real_estate_property.py`
164164
165165
from odoo import fields, models
166-
from odoo.tools import date_utils
167166
168167
169168
class RealEstateProperty(models.Model):
@@ -200,15 +199,16 @@ create a model with some fields to represent real estate properties and their ch
200199
selling_price = fields.Float(
201200
string="Selling Price", help="The selling price excluding taxes.", required=True
202201
)
203-
availability_date = fields.Date(
204-
string="Availability Date", default=date_utils.add(fields.Date.today(), months=2)
205-
)
202+
availability_date = fields.Date(string="Availability Date")
206203
floor_area = fields.Integer(
207204
string="Floor Area", help="The floor area in square meters excluding the garden."
208205
)
209206
bedrooms = fields.Integer(string="Number of bedrooms", default=2)
210-
has_garden = fields.Boolean(string="Garden")
211207
has_garage = fields.Boolean(string="Garage")
208+
has_garden = fields.Boolean(string="Garden")
209+
garden_area = fields.Integer(
210+
string="Garden Area", help="The garden area excluding the building."
211+
)
212212
213213
Congrats, you have just defined the first model of our real estate app! However, the changes have
214214
not yet been applied to the database. To do so, you must add the `-u real_estate` argument to the
@@ -235,7 +235,6 @@ you created translate into a new SQL table. We will use `psql`, the CLI
235235
:dfn:`command-line interface` allowing to browse and interact with PostgreSQL databases.
236236

237237
.. exercise::
238-
239238
#. In your terminal, execute the command :command:`psql -d tutorials`.
240239
#. Enter the command :command:`\\d real_estate_property` to print the description of the
241240
`real_estate_property` table.
@@ -246,6 +245,7 @@ you created translate into a new SQL table. We will use `psql`, the CLI
246245
.. spoiler:: Solution
247246

248247
.. code-block:: text
248+
:caption: terminal
249249
250250
$ psql -d tutorials
251251
@@ -256,6 +256,7 @@ you created translate into a new SQL table. We will use `psql`, the CLI
256256
id | integer | | not null | nextval('real_estate_property_id_seq'::regclass)
257257
floor_area | integer | | |
258258
bedrooms | integer | | |
259+
garden_area | integer | | |
259260
create_uid | integer | | |
260261
write_uid | integer | | |
261262
name | character varying | | not null |
@@ -264,8 +265,8 @@ you created translate into a new SQL table. We will use `psql`, the CLI
264265
availability_date | date | | |
265266
description | text | | |
266267
active | boolean | | |
267-
has_garden | boolean | | |
268268
has_garage | boolean | | |
269+
has_garden | boolean | | |
269270
create_date | timestamp without time zone | | |
270271
write_date | timestamp without time zone | | |
271272
selling_price | double precision | | not null |
@@ -343,9 +344,6 @@ The most common data operation is creating new records through the `record` and
343344
elements, but other operations exist, such as `delete`, which deletes previously created records, or
344345
even `function`, which allows executing arbitrary code.
345346

346-
.. seealso::
347-
:doc:`Reference documentation for XML data files <../../reference/backend/data>`
348-
349347
Some data operations require their data elements to be uniquely identified by the system. This is
350348
achieved by means of the `id` attribute, also known as the **XML ID** or **external identifier**. It
351349
provides a way for other elements to reference it with the `ref` attribute and links data elements
@@ -387,10 +385,12 @@ created from a data file so that records can be referenced by their full XML ID
387385
- The `ref` attribute is used to reference other records by their XML ID and use their record
388386
ID as value.
389387

388+
.. seealso::
389+
:doc:`Reference documentation for XML data files <../../reference/backend/data>`
390+
390391
Let's now load some default real estate properties in our database.
391392

392393
.. exercise::
393-
394394
#. Create a new :file:`real_estate_property_data.xml` file at the root of the `real_estate`
395395
module.
396396
#. Update the manifest to let the server know that it should load our data file. To do so, have
@@ -441,10 +441,12 @@ Let's now load some default real estate properties in our database.
441441
<field name="image" type="base64" file="real_estate/country_house.png"/>
442442
<field name="type">house</field>
443443
<field name="selling_price">745000</field>
444+
<field name="availability_date">2024-08-01</field>
444445
<field name="floor_area">416</field>
445446
<field name="bedrooms">5</field>
446-
<field name="has_garden">True</field>
447447
<field name="has_garage">True</field>
448+
<field name="has_garden">True</field>
449+
<field name="garden_area">2100</field>
448450
</record>
449451
450452
<record id="real_estate.loft" model="real.estate.property">
@@ -456,8 +458,8 @@ Let's now load some default real estate properties in our database.
456458
<field name="availability_date">2025-01-01</field>
457459
<field name="floor_area">195</field>
458460
<field name="bedrooms">3</field>
459-
<field name="has_garden">False</field>
460461
<field name="has_garage">True</field>
462+
<field name="has_garden">False</field>
461463
</record>
462464
463465
<record id="real_estate.mixed_use_commercial" model="real.estate.property">
@@ -469,8 +471,8 @@ Let's now load some default real estate properties in our database.
469471
<field name="availability_date">2024-10-02</field>
470472
<field name="floor_area">370</field>
471473
<field name="bedrooms">0</field>
472-
<field name="has_garden">False</field>
473474
<field name="has_garage">False</field>
475+
<field name="has_garden">False</field>
474476
</record>
475477
476478
</odoo>
@@ -484,9 +486,6 @@ In addition to XML data files, the server framework allows loading data files in
484486
format is often more convenient for describing records with simple field values belonging to the
485487
same model. It also loads faster, making it the go-to format when performance matters most.
486488

487-
.. seealso::
488-
:ref:`Reference documentation for CSV data files <reference/data/csvdatafiles>`
489-
490489
.. example::
491490
See below for an example of how a subset of `country states can be loaded into Odoo
492491
<{GITHUB_PATH}/odoo/addons/base/data/res.country.state.csv>`_.
@@ -510,14 +509,16 @@ same model. It also loads faster, making it the go-to format when performance ma
510509
state_ca_yt,ca,"Yukon","YT"
511510
512511
.. note::
513-
514512
- The file name must match the model name.
515513
- The first line lists the model fields to populate.
516514
- XML IDs are specified via the special `id` field.
517515
- The `:id` suffix is used to reference other records by their XML ID and use their record ID
518516
as value.
519517
- Each subsequent line describes one new record.
520518

519+
.. seealso::
520+
:ref:`Reference documentation for CSV data files <reference/data/csvdatafiles>`
521+
521522
In business applications like Odoo, one of the first questions to consider is who can access the
522523
data. By default, access to newly created models is restricted until it is explicitly granted.
523524
Granting access rights is done by creating records of the `ir.model.access` model, which specifies
@@ -532,7 +533,6 @@ began being logged at server start-up after creating the model:
532533
WARNING tutorials odoo.modules.loading: The models ['real.estate.property'] have no access rules [...]
533534
534535
.. exercise::
535-
536536
#. Create a new :file:`ir.model.access.csv` file at the root of the `real_estate` module.
537537
#. Declare it in the manifest as you did for the :file:`real_estate_property_data.xml` file.
538538
#. Grant access to the `real.estate.property` model to all administrators of the database by
@@ -550,7 +550,7 @@ began being logged at server start-up after creating the model:
550550

551551
.. spoiler:: Solution
552552

553-
.. code-block:: py
553+
.. code-block:: python
554554
:caption: `__manifest__.py`
555555
:emphasize-lines: 2
556556

0 commit comments

Comments
 (0)
0