@@ -24,7 +24,7 @@ when some values are derived from other fields. Fortunately, the server framewor
24
24
ability to define **computed fields **.
25
25
26
26
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
28
28
the field is accessed. This makes computed fields highly convenient for tasks such as displaying
29
29
calculation results in views, automating repetitive processes, or assisting users during data entry.
30
30
@@ -37,7 +37,6 @@ the records to compute and set the field's value for each one.
37
37
- :ref: `Reference documentation for computed fields <reference/fields/compute >`
38
38
- :ref: `Reference documentation for recordsets <reference/orm/recordsets >`
39
39
40
- .. todo: reference doc on recordsets
41
40
.. todo: compute: offer deadline
42
41
.. todo: For relational fields, it’s possible to use paths through a field as a dependency: @api.depends('partner_id.name')
43
42
.. todo: methods are private by default, meaning that they can't be called from the presentation
@@ -206,52 +205,67 @@ the constraint is violated.
206
205
Set default field values
207
206
========================
208
207
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.
211
211
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.
220
217
221
218
.. 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))
222
220
.. todo : real.estate.offer.amount ::default -> property.selling_price (add related?)
223
221
.. todo: real.estate.tag.color -> default=_default_color ; def _default_color(self): return random.randint(1, 11) (check if lambda works)
224
222
.. todo: copy=False on some fields
225
223
226
- .. _tutorials/server_framework_101/actions :
224
+ .. _tutorials/server_framework_101/action_buttons :
227
225
228
226
Trigger business workflows
229
227
==========================
230
228
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
+
231
234
.. todo: "assign myself as salesperson" action
232
235
.. todo: "view best offer" statbutton
233
236
.. todo: accept/refuse offer buttons
234
237
.. todo: action name=...
235
238
236
- tmp
239
+ .. _ tutorials/server_framework_101/action_type_actions :
237
240
238
- .. _tutorials/server_framework_101/action_object :
241
+ XML-defined actions
242
+ -------------------
239
243
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.
242
247
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.
244
252
245
- tmp
253
+ .. todo: ref to `reference/view_architectures/form/button`
254
+ .. todo: ref to `reference/view_architectures/form/header`
246
255
247
- .. _tutorials/server_framework_101/action_name :
256
+ .. _tutorials/server_framework_101/object_type_actions :
248
257
249
- Name
250
- ----
258
+ Model-defined actions
259
+ ---------------------
251
260
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.
253
264
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.
255
269
256
270
.. _tutorials/server_framework_101/shell :
257
271
@@ -260,6 +274,8 @@ Use the interactive shell
260
274
261
275
tmp
262
276
277
+ .. todo: talk about env.cr.commit()
278
+
263
279
----
264
280
265
281
.. todo: add incentive for chapter 6
0 commit comments