I have 2 computed (not stored) monetary fields on my crm.lead model. I want the grouped sums to appear, when I group the leads (based on some other fields on the model).
My XML code has:
<field name="my_deal_share" sum="My Deal Share" widget="monetary" options="{'currency_field': 'company_currency'}" />
<field name="my_profit_share" sum="My Profit Share" widget="monetary" options="{'currency_field': 'company_currency'}" />
I have used the following Python code in my model:
@api.model
def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
_logger.info("Entering read_group method for CrmLead")
_logger.info(f"Domain: {domain}")
_logger.info(f"Fields requested: {fields}") # Check if ':sum' is present here
_logger.info(f"Groupby: {groupby}")
# Call the original read_group first to get the grouped data
res = super(CrmLead, self).read_group(domain, fields, groupby, offset=offset, limit=limit, orderby=orderby, lazy=lazy)
_logger.info(f"Original read_group result (first 2 groups): {res[:2]}")
# Determine which computed fields need summing
needs_deal_share_sum = 'my_deal_share:sum' in fields
needs_profit_share_sum = 'my_profit_share:sum' in fields
_logger.info(f"Needs Deal Share Sum: {needs_deal_share_sum}")
_logger.info(f"Needs Profit Share Sum: {needs_profit_share_sum}")
# Only proceed if at least one of the computed fields' sums is requested
if needs_deal_share_sum or needs_profit_share_sum:
for group in res:
# Get the records for this specific group
group_domain = domain + (group.get(groupby[0] + '_domain') if groupby else [])
records_in_group = self.search(group_domain)
_logger.info(f"Processing group: {group.get(groupby[0])}")
_logger.info(f"Records in group count: {len(records_in_group)}")
# Calculate and set the sum for my_deal_share if requested
if needs_deal_share_sum:
computed_my_deal_share_sum = sum(rec.my_deal_share for rec in records_in_group)
group['my_deal_share'] = computed_my_deal_share_sum
_logger.info(f"Computed my_deal_share_sum: {computed_my_deal_share_sum}")
# Calculate and set the sum for my_profit_share if requested
if needs_profit_share_sum:
computed_my_profit_share_sum = sum(rec.my_profit_share for rec in records_in_group)
group['my_profit_share'] = computed_my_profit_share_sum
_logger.info(f"Computed my_profit_share_sum: {computed_my_profit_share_sum}")
_logger.info(f"Final read_group result (first 2 groups) after custom sums: {res[:2]}")
return res
When I check logs, I see:
2025-06-20 08:01:42,196 7188 INFO db3 odoo.addons.custom_crm.models.crm_lead: Needs Deal Share Sum: False
2025-06-20 08:01:42,196 7188 INFO db3 odoo.addons.custom_crm.models.crm_lead: Needs Profit Share Sum: False
Thus, these grouped sums are not calculated.
How can I resolve this?
Odoo v18 Community Edition