@@ -192,88 +192,6 @@ access_custom_partner_note,custom.partner.note,base.model_res_partner,base.group
192
192
193
193
## Advanced Customization Examples
194
194
195
- ### Adding Computed Fields
196
-
197
- You can add computed fields on ` res.partner ` for both Individuals and Groups. Below are simple examples:
198
-
199
- ``` python
200
- from odoo import fields, models, api
201
-
202
- class G2PRegistrant (models .Model ):
203
- _inherit = " res.partner"
204
-
205
- # Individual indicator: age in years (0 for groups or missing birthdate)
206
- z_ind_indv_age_years = fields.Integer(
207
- string = " Age (years)" ,
208
- compute = " _compute_indv_age_years" ,
209
- store = True ,
210
- help = " Computed age in years for individuals" ,
211
- )
212
-
213
- # Group indicator: number of members (0 for individuals)
214
- z_ind_grp_member_count = fields.Integer(
215
- string = " Group Member Count" ,
216
- compute = " _compute_grp_member_count" ,
217
- store = True ,
218
- help = " Computed number of members for groups" ,
219
- )
220
-
221
- @api.depends (" birthdate" )
222
- def _compute_indv_age_years (self ):
223
- today = fields.Date.context_today(self )
224
- for partner in self :
225
- if partner.is_group or not partner.birthdate:
226
- partner.z_ind_indv_age_years = 0
227
- continue
228
- age = today.year - partner.birthdate.year - (
229
- (today.month, today.day) < (partner.birthdate.month, partner.birthdate.day)
230
- )
231
- partner.z_ind_indv_age_years = max (age, 0 )
232
-
233
- @api.depends (" group_membership_ids.individual" )
234
- def _compute_grp_member_count (self ):
235
- for partner in self :
236
- partner.z_ind_grp_member_count = (
237
- len (partner.group_membership_ids) if partner.is_group else 0
238
- )
239
- ```
240
-
241
- Optionally expose the computed fields in the UI:
242
-
243
- ``` xml
244
- <odoo >
245
- <!-- Individual form: show computed age -->
246
- <record id =" view_individuals_form_custom_indicators" model =" ir.ui.view" >
247
- <field name =" name" >view_individuals_form_custom_indicators</field >
248
- <field name =" model" >res.partner</field >
249
- <field name =" inherit_id" ref =" g2p_registry_individual.view_individuals_form" />
250
- <field name =" arch" type =" xml" >
251
- <xpath expr =" //page[@name='basic_info']/group/group[1]" position =" after" >
252
- <group colspan =" 2" >
253
- <field name =" z_ind_indv_age_years" />
254
- </group >
255
- </xpath >
256
- </field >
257
- </record >
258
-
259
- <!-- Group form: show member count -->
260
- <record id =" view_groups_form_custom_indicators" model =" ir.ui.view" >
261
- <field name =" name" >view_groups_form_custom_indicators</field >
262
- <field name =" model" >res.partner</field >
263
- <field name =" inherit_id" ref =" g2p_registry_group.view_groups_form" />
264
- <field name =" arch" type =" xml" >
265
- <xpath expr =" //page[@name='basic_info']/group/group[1]" position =" after" >
266
- <group colspan =" 2" >
267
- <field name =" z_ind_grp_member_count" />
268
- </group >
269
- </xpath >
270
- </field >
271
- </record >
272
- </odoo >
273
- ```
274
-
275
- Performance note: for high-volume indicators, consider batch recomputation strategies similar to those used in OpenSPP indicator modules instead of heavy ` @api.depends ` chains.
276
-
277
195
## Adding a One2many Field (creates a new model)
278
196
279
197
To relate multiple records to a registrant, add a One2many on ` res.partner ` and define its comodel. Because this introduces a new model, include proper access rights and reference the G2P admin group from ` g2p_registry_base ` .
0 commit comments