8000 tmp · odoo/documentation@1fc06be · GitHub
[go: up one dir, main page]

Skip to content

Commit 1fc06be

Browse files
committed
tmp
1 parent 39d992a commit 1fc06be

File tree

3 files changed

+50
-32
lines changed

3 files changed

+50
-32
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ 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
@@ -140,7 +140,7 @@ create a model with some fields to represent real estate properties and their ch
140140
- Type (house, apartment, office building, retail space, or warehouse; required; default to
141141
house)
142142
- Selling Price (without currency; with help text; required)
143-
- Availability Date (default to creation date + two months)
143+
- Availability Date
144144
- Floor Area (in square meters; with help text)
145145
- Number of Bedrooms (default to two)
146146
- Whether there is a garden
@@ -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,9 +199,7 @@ 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
)

content/developer/tutorials/server_framework_101/04_relational_fields.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,11 @@ to a list of offers received from potential buyers.
646646
#. Allow connecting properties to multiple offers.
647647
#. Modify the form view of properties to display offers in a new notebook page titled "Offers".
648648

649+
.. tip::
650+
The `default` field argument expects a callable function, not a precalculated value. If you
651+
mistakenly pass the result of calling the `fields.Date.today` helper function, the field's
652+
default value will be set to the server's start-up time, not the correct date at runtime.
653+
649654
.. spoiler:: Solution
650655

651656
.. code-block:: python
@@ -660,7 +665,7 @@ to a list of offers received from potential buyers.
660665
661666
amount = fields.Float(string="Amount", required=True)
662667
buyer_id = fields.Many2one(string="Buyer", comodel_name='res.partner', required=True)
663-
date = fields.Date(string="Date", required=True, default=fields.Date.today())
668+
date = fields.Date(string="Date", required=True, default=fields.Date.today)
664669
validity = fields.Integer(
665670
string="Validity", help="The number of days before the offer expires.", default=7
666671
)

content/developer/tutorials/server_framework_101/05_connect_the_dots.rst

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ when some values are derived from other fields. Fortunately, the server framewor
2424
ability to define **computed fields**.
2525

2626
Computed fields are a special type of field whose value are derived through programmatic computation
27-
rather than stored directly in the database. The servers computes these values on the fly whenever
27+
rather than stored directly in the database. The server computes these values on the fly whenever
2828
the field is accessed. This makes computed fields highly convenient for tasks such as displaying
2929
calculation results in views, automating repetitive processes, or assisting users during data entry.
3030

@@ -37,7 +37,6 @@ the records to compute and set the field's value for each one.
3737
- :ref:`Reference documentation for computed fields <reference/fields/compute>`
3838
- :ref:`Reference documentation for recordsets <reference/orm/recordsets>`
3939

40-
.. todo: reference doc on recordsets
4140
.. todo: compute: offer deadline
4241
.. todo: For relational fields, it’s possible to use paths through a field as a dependency: @api.depends('partner_id.name')
4342
.. todo: methods are private by default, meaning that they can't be called from the presentation
@@ -206,52 +205,67 @@ the constraint is violated.
206205
Set default field values
207206
========================
208207

209-
.. todo: introduce lambda functions and fields.Date.today for defaults :point_down:
210-
also mention that `self` is evaluated as the current recordset in lambda functions
208+
When creating new records, pre-filling certain fields with default values can simplify data entry
209+
and reduces the likelihood of errors. Defaults are particularly useful when values are derived from
210+
the system or context, such as the current date, time, or logged-in user.
211211

212-
There is a problem with the way we defined our `Date` fields in the previous chapters: their default value relies on
213-
:code:`fields.Date.today()` or some other static method. When the code is loaded into memory, the date is
214-
computed once and reused for all newly created records until the server is shut down. You probably didn't
215-
notice it, unless you kept your server running for several days, but it would be much more visible with
216-
`Datetime` fields, as all newly created records would share the same timestamp.
217-
218-
That's where lambda functions come in handy. As they generate an anonymous function each time they're evaluated
219-
at runtime, they can be used in the computation of default field values to return an updated value for each new record.
212+
Fields can be assigned a default value by including the `default` argument in their declaration.
213+
This argument can be set to a static value or dynamically generated using a callable function, such
214+
as a model method or a lambda function. In both cases, the `self` argument provides access to the
215+
environment but does not represent the current record, as no record exists yet during the creation
216+
process.
220217

221218
.. todo: salesperson_id = fields.Many2one(default=lambda self: self.env.user)
219+
.. todo: availability_date = fields.Date(default=lambda self: date_utils.add(fields.Date.today(), months=2))
222220
.. todo: real.estate.offer.amount::default -> property.selling_price (add related?)
223221
.. todo: real.estate.tag.color -> default=_default_color ; def _default_color(self): return random.randint(1, 11) (check if lambda works)
224222
.. todo: copy=False on some fields
225223
226-
.. _tutorials/server_framework_101/actions:
224+
.. _tutorials/server_framework_101/action_buttons:
227225

228226
Trigger business workflows
229227
==========================
230228

229+
**Action buttons** allow users to trigger specific workflows directly from the user interface. These
230+
buttons can be of type **action**, defined in XML, or **object**, implemented in the model.
231+
Together, these types of buttons facilitate the integration of user interactions with business
232+
logic.
233+
231234
.. todo: "assign myself as salesperson" action
232235
.. todo: "view best offer" statbutton
233236
.. todo: accept/refuse offer buttons
234237
.. todo: action name=...
235238
236-
tmp
239+
.. _tutorials/server_framework_101/action_type_actions:
237240

238-
.. _tutorials/server_framework_101/action_object:
241+
XML-defined actions
242+
-------------------
239243

240-
Object
241-
------
244+
Action-type buttons link to actions defined in XML and are typically used to display specific views
245+
or trigger server actions. These buttons allow developers to link workflows to the UI without
246+
writing Python code, making them ideal for simple, preconfigured tasks.
242247

243-
.. todo: change section title
248+
We already saw :ref:`how to link XML-defined window actions to menu items
249+
<tutorials/server_framework_101/define_window_actions>`. To link a button to an XML-defined action,
250+
a `button` element must be added to the view, with its `type` attribute set to `action`. The `name`
251+
attribute should reference the XML ID of the action to execute.
244252

245-
tmp
253+
.. todo: ref to `reference/view_architectures/form/button`
254+
.. todo: ref to `reference/view_architectures/form/header`
246255
247-
.. _tutorials/server_framework_101/action_name:
256+
.. _tutorials/server_framework_101/object_type_actions:
248257

249-
Name
250-
----
258+
Model-defined actions
259+
---------------------
251260

252-
.. todo: change section title
261+
Object-type buttons link to model methods that execute custom business logic. These methods enable
262+
more complex workflows, such as processing the current records, configuring actions depending on
263+
these records, or integrating with external systems.
253264

254-
tmp
265+
To link a button to a model-defined action, its `type` attribute must be set to `object`, and its
266+
`name` attribute must be set to the name of the model method to call when the button is clicked. The
267+
method receives the current recordset through `self` and should return a dictionary acting as an
268+
action descriptor.
255269

256270
.. _tutorials/server_framework_101/shell:
257271

@@ -260,6 +274,8 @@ Use the interactive shell
260274

261275
tmp
262276

277+
.. todo: talk about env.cr.commit()
278+
263279
----
264280

265281
.. todo: add incentive for chapter 6

0 commit comments

Comments
 (0)
0