KEMBAR78
How to use mapped() method in Odoo 18 | PPTX
How to use mapped() method in Odoo 18
Enterprise
Enterprise
Introduction
In this slide we’ll discuss on how to use mapped() method in
Odoo 18. The mapped() method in Odoo is a powerful tool for
simplifying data extraction and transformation from recordsets.
It allows developers to retrieve values of specific fields or apply
operations across related records efficiently. By chaining field
names or method calls, you can work with complex data
structures in a concise manner. This makes mapped() an
essential method for writing clean, optimized, and Pythonic
code in Odoo 18.
Enterprise
Using Mapped methods reduces the need for nested loops and
optimizes the code. We can consider an example of computing the
total fees in a custom module.
fee_detail_ids = fields.One2many('student.fee.detail',
'student_id',
string='Fee Details'
)
fee_detail_ids is a one2many field where we add the details of the
fee for each student. We need to get the sum of all fees for each
student as a total in the student.detail model. For that we can use
the mapped method for finding the total of the student fee detail
lines.
Enterprise
total_fee = fields.Float(
string='Total Fee',
compute='_compute_total_fee',
store=True
)
We have defined a field total_fee in student.detail model to compute
the total of all fees for each student. After defining this field, we can
define the compute function for this field in our model using the
mapped() method.
Enterprise
@api.depends('fee_detail_ids.amount')
def _compute_total_fee(self):
for record in self:
record.total_fee =
sum(record.fee_detail_ids.mapped('amount'))
This code defines a computed field _compute_total_fee that
calculates the total fee for each student based on the amount field
in the related fee_detail_ids records. The mapped('amount')
function is used to efficiently extract and sum all amount values
from the related student.fee.detail records for each student.detail.
Enterprise
Here we can see the total is computed using the mapped() method
from the amount in fee details model.
For More Info.
Check our company website for related blogs
and Odoo book.
Check our YouTube channel for
functional and technical videos in Odoo.
Enterprise
www.cybrosys.com

How to use mapped() method in Odoo 18

  • 1.
    How to usemapped() method in Odoo 18 Enterprise
  • 2.
    Enterprise Introduction In this slidewe’ll discuss on how to use mapped() method in Odoo 18. The mapped() method in Odoo is a powerful tool for simplifying data extraction and transformation from recordsets. It allows developers to retrieve values of specific fields or apply operations across related records efficiently. By chaining field names or method calls, you can work with complex data structures in a concise manner. This makes mapped() an essential method for writing clean, optimized, and Pythonic code in Odoo 18.
  • 3.
    Enterprise Using Mapped methodsreduces the need for nested loops and optimizes the code. We can consider an example of computing the total fees in a custom module. fee_detail_ids = fields.One2many('student.fee.detail', 'student_id', string='Fee Details' ) fee_detail_ids is a one2many field where we add the details of the fee for each student. We need to get the sum of all fees for each student as a total in the student.detail model. For that we can use the mapped method for finding the total of the student fee detail lines.
  • 4.
    Enterprise total_fee = fields.Float( string='TotalFee', compute='_compute_total_fee', store=True ) We have defined a field total_fee in student.detail model to compute the total of all fees for each student. After defining this field, we can define the compute function for this field in our model using the mapped() method.
  • 5.
    Enterprise @api.depends('fee_detail_ids.amount') def _compute_total_fee(self): for recordin self: record.total_fee = sum(record.fee_detail_ids.mapped('amount')) This code defines a computed field _compute_total_fee that calculates the total fee for each student based on the amount field in the related fee_detail_ids records. The mapped('amount') function is used to efficiently extract and sum all amount values from the related student.fee.detail records for each student.detail.
  • 6.
    Enterprise Here we cansee the total is computed using the mapped() method from the amount in fee details model.
  • 7.
    For More Info. Checkour company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com